请问:如何实现从select结果集中选择其中的几条记录?

xiaocaohm 2003-08-23 09:19:17
比如select * from table where condition我想从选出的这个结果集中选出其中的第100到200条记录可以吗?

我知道可以用top选出前面的几条 select top 100 * from table;
但是现在要选中间的条数,不知道该怎么办?(最好不要产生临时表)
谢谢
...全文
49 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaocaohm 2003-08-25
  • 打赏
  • 举报
回复
谢谢各位大虾,问题解决了。
happy_0325 2003-08-23
  • 打赏
  • 举报
回复
那你就用临时表吧
select identy(1,1) id,* into #table from table where ..
select * from #table where id betweent 3 and 10 and exp
eminena 2003-08-23
  • 打赏
  • 举报
回复
triout(笨牛)的方法不简炼!
xiaocaohm 2003-08-23
  • 打赏
  • 举报
回复
感谢各位大虾。
triout 2003-08-23
  • 打赏
  • 举报
回复
select * from (select top 200 * from table where condition) b where b.id not in
(select top 100 id from table where condition)

这里的ID,城如楼上的所说,如果没有,该怎么办?其实很简单,所谓ID ,只不过是用于唯一标识一条记录而已,你使用你的关键字就可以了。

也许问题又出来了,我的关键字由两个字段组成,怎么办?还是一样的,小小的变通吗:
select * from (select top 200 * from table where condition) b where b.id1 not in
(select top 100 id1 from table where condition) and b.id2 not in (select top 100 id2 from table where condition)

我想你不会再问三个字段组成的关键字时该如何写了吧?
ysycrazy 2003-08-23
  • 打赏
  • 举报
回复
erigido(丰田村农民) :他没有记录号怎么办?如果有记录号,我觉得你的方法也不是最好的!
ysycrazy 2003-08-23
  • 打赏
  • 举报
回复
如果用临时表很简单,不用我也不知道!
erigido 2003-08-23
  • 打赏
  • 举报
回复
select top 200 * from table
where id not in(select top 100 id from table)
dreamreality 2003-08-23
  • 打赏
  • 举报
回复
可以这样的,但我个人认为不怎么好,还是临时表好
修此表的结构,新增一个字段,设为自动加1,然后就可以用erigido(丰田村农民)的方法
最后再删除这个新增的字段,

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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