如何从表中查询出第几条到第几条数据?

Atest001 2011-02-24 03:28:34
有一张学生表,里面有很多的数据,如何从表中取出第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条数据吗?
...全文
199 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 2011-02-24
  • 打赏
  • 举报
回复

--2000
select rn = identity(int,1,1),* into #temp
from tb

select *
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的取出来。
快溜 2011-02-24
  • 打赏
  • 举报
回复
 select top 3 * from tb where id not in(select top 2 id from tb)
Linares 2011-02-24
  • 打赏
  • 举报
回复
row_number

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧