sql查询问题!!想取到第a条与第b条中间的(包括a)的所有数据,怎么写???

iceman09 2003-05-16 09:54:59
sql查询问题!!想取到第a条与第b条中间的(包括a)的所有数据,怎么写???
(a<b)a,b是从外面传进来的整形变量。
...全文
103 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
19191919 2003-05-16
  • 打赏
  • 举报
回复
select top b-a * from yourTable where id not in(select top a-1 id from table)
ID为表具有唯一值的任何字段或任何字段的组合
同时注意top 后面不支持参数,如果含参数执行要用exec
exec('select top '+@b-@a 的变量 +' * from yourTable where id not in(select top' + @a-1 的变量+' id from table)'
)
如果没有唯一值,按照蚂蚁的就可以了
psxfghost 2003-05-16
  • 打赏
  • 举报
回复
更正:
select * from (select identity(1,1) as id,top b * from table) a where a.id not in(select b.id from (select identity(1,1) as id, top a * from table) as b)

或者
select identity(1,1) as id, * into #temp from table
select * from #temp where id>=a and id<=b
yoki 2003-05-16
  • 打赏
  • 举报
回复
方法一:取出前n条和前m条然后取出其差(n-m)
select top(n-m) *
from table
where 关键字段 not in(select top m 关键字段 from table)

方法二:先取出前n条的,再倒过来取出n-m就可以
select top(n-m+1) *
from (select top n * from table order by 关键字段 desc)
order by 关键字段
CrazyFor 2003-05-16
  • 打赏
  • 举报
回复


查询N-M条记录。
select IDENTITY(int,1,1) as iid,* into #temptable from yourtable
select top M-N * from #temptable where iid>=N

OR:

select top M-N * from yourTable where id not in(select top N-1 id from table)
ID为表具有唯一值的任何字段
psxfghost 2003-05-16
  • 打赏
  • 举报
回复
select * from (select identity(1,1) as id,top b * from table) a where a.id not in(select identity(1,1) as id top a id from table)

34,837

社区成员

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

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