有一张学生表,里面有很多的数据,如何从表中取出第2条到第5条数据?
name age sex prof id test1 22 男 计算机 121
test2 22 男 计算机 123
test3 22 男 计算机 124
test4 22 男 计算机 211
test5 22 男 计算机 985
test6 22 男 计算机 101
其中id是主键。
有人说这样子不可以做到查询出第二条到第五条数据,因为id值不是连续的数字,如果是101,102,103…… 就可以用 select * from table where id between 102 and 105。
如上表,可以查询出第2条到第5条数据吗?
name age sex prof id test1 22 男 计算机 121
test2 22 男 计算机 123
test3 22 男 计算机 124
test4 22 男 计算机 211
test5 22 男 计算机 985
test6 22 男 计算机 101
其中id是主键。
有人说这样子不可以做到查询出第二条到第五条数据,因为id值不是连续的数字,如果是101,102,103…… 就可以用 select * from table where id between 102 and 105。
如上表,可以查询出第2条到第5条数据吗?
--2000
select rn = identity(int,1,1),* into #temp
from tbselect *
from #temp
where rn between 3 and 5 -- 3到5的取出来。--2005
select *
from(select rn = row_number() over (order by getdate()),* from tb)u
where rn between 3 and 5 -- 3到5的取出来。