一个查询的问题

zhangns 2004-08-27 03:02:08
我们在查询记录集前50条记录时,可以用 top 50
那我要查询第20至50条,应该如何写呢?
...全文
105 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsxaa 2004-08-27
  • 打赏
  • 举报
回复
select top 30 * from (select top 50 * from table order 排序字段) order by 排序字段 desc
xiyang2170 2004-08-27
  • 打赏
  • 举报
回复
Select Top 40 F.*
From F
Where F.ID NOT IN (Select Top 10 A.ID
From A
Order By A.CreateTime ASC) --表F中的ID不等于表A中的前10条
Order BY F.CreateTime ASC
Decay 2004-08-27
  • 打赏
  • 举报
回复
假如表中有字段ID
select top 31 from table where ID not in (select top 19 id from table)
LoveSQL 2004-08-27
  • 打赏
  • 举报
回复
select identity(int,1,1) as sid,* into #tmp from yourtable

select * from #tmp where sid between 20 and 50
qgbin 2004-08-27
  • 打赏
  • 举报
回复
select identity(int,1,1) as xh,* into #a
from TB
select * from #a where xh>=20 and xh<=50
老宛 2004-08-27
  • 打赏
  • 举报
回复
查询N-M条记录?

描述:要从记录集中取出中间一段记录来(如: 取10条中的第3-8条)

解决思路:

1. 有关键字或确保唯一的候选关键字字段:

方法1:从第n条记录开始取m-n+1条数据

方法2:升序取前m条记录最为a, 再降序从a中取m-n+1条记录作为b

2. 没有上述字段:

增加一个自增字段生成一个临时表,在从临时表中取记录[Where 自增字段名>n-1]

实例: --

--取第4到第7条记录 [对1方法一]

SELECT top 4 * FROM jobs WHERE job_id not in (SELECT top 3 job_id FROM jobs)

--[方法二]

SELECT top 4 * FROM

(SELECT top 4 * FROM

(SELECT top 7 * FROM jobs ORDER BY job_id ASC) a

ORDER BY job_id DESC

) b

ORDER BY job_id ASC

--取第2到第3条记录 [对1方法一]

select IDENTITY(int,1,1) as iid,* into #temptable from discounts select top 2 * from #temptable where iid>=2

34,873

社区成员

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

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