SQL语句查询问题

zhjxingxing 2003-12-16 03:30:04
我想分十次得到查询结果
select * from 表名 where (其他条件) and id in (select top 20 id from 表名)
这样只能得到头20行里查询的结果,如何得到20至40行的结果,及40至60行等等
...全文
17 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rotaxe 2003-12-16
  • 打赏
  • 举报
回复
select top 20 * from 表名 where (其他条件) and id in (select top 40 id from 表名 where (其他条件) order by id ) order by id desc
wzh1215 2003-12-16
  • 打赏
  • 举报
回复
楼上的错了,如果一定要用变量的话,也只能用动态语句执行!
exec('select top '+@PAGES+' * from 表名 where (其他条件)
and id NOT in (select top '+@NO*20+' id from 表名 where (其他条件))')
victorycyz 2003-12-16
  • 打赏
  • 举报
回复
select top 21 * from tablename a left join (select top 19 id from tablename) b on a.id=b.id where b.id isnull
1ssp 2003-12-16
  • 打赏
  • 举报
回复
DECLARE @PAGES INT,@NO INT
set @pages=20
set @no=2
--@PAGES 行数,@NO 第几页
select top @PAGES * from 表名 where (其他条件)
and id NOT in (select top @NO*20 id from 表名 where (其他条件) )
1ssp 2003-12-16
  • 打赏
  • 举报
回复
DECLARE @PAGES INT,@NO INT
@PAGES 行数,@NO 第几页
select top @PAGES * from 表名 where (其他条件)
and id NOT in (select top @NO*20 id from 表名 where (其他条件) )
wzh1215 2003-12-16
  • 打赏
  • 举报
回复
20-40
select * from 表名 where (其他条件) and id in (select top 20 id from 表名 where id not in (select top 20 id from 表名))
40-60
select * from 表名 where (其他条件) and id in (select top 20 id from 表名 where id not in (select top 40 id from 表名))

34,592

社区成员

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

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