读书人

在论坛中出现的比较难的sql有关问题:5

发布时间: 2013-10-22 16:17:14 作者: rapoo

在论坛中出现的比较难的sql问题:5
最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了。

所以,觉得有必要记录下来,这样以后再次碰到这类问题,也能从中获取解答的思路。


1、内连接后,如何分页。

http://bbs.csdn.net/topics/390617884


sqlserver 对一张表进行分页查询,但是还要通过第二章张表获取信息
比如有一张表
create table Student(
sid int primary key identity(1,1) ,
sname varchar(15) not null
)
第二张表
create table Comment(
id int primary key identity(1,1) ,
sid int not null
)
我需要对第二张表comment 进行分页查询,但是还要通过第一张表查询姓名
要改如何写sql代码

我用内连接后接下来该如何做。

我的解法:

if object_id('tb') is not null drop table tbgo create table tb([id] int,[trueName] varchar(4),[userName] varchar(3),[score] int)insert tbselect 1,'张三','zs',80 union allselect 2,'张三','zs1',100 union allselect 3,'张三','zs2',90 union allselect 1,'李四','zs',80 union allselect 2,'李四','zs1',100 union allselect 3,'李四','zs2',90--第1次运行select *from(select truename,       username,       row_number() over(partition by truename order by checksum(newid())) as rownum,       sum(score) over(partition by truename) as scorefrom tb)awhere a.rownum = 1/*truename username rownum               score-------- -------- -------------------- -----------李四       zs1      1                    270张三       zs2      1                    270*/--第2次运行/*truename username rownum               score-------- -------- -------------------- -----------李四       zs2      1                    270张三       zs       1                    270*/




读书人网 >SQL Server

热点推荐