ms sql server 怎么查询指定行数的记录

fengsky491 2009-04-01 06:01:22
有表(主键为自动增长列,但可能不连续),
问:
怎么查询指定行数的数据?
如,怎么取得21行到29行的数据?
...全文
2485 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
kxshflyingbird 2010-05-10
  • 打赏
  • 举报
回复
study
Xiaowuasa 2009-04-02
  • 打赏
  • 举报
回复
select * from (
select *,px=(select count(1) from tb where 主键<a.主键)+1 from tb a) b
where px between 21 and 29
fengsky491 2009-04-02
  • 打赏
  • 举报
回复
好多答案啊,一会我去测试去,呵呵。
fengsky491 2009-04-02
  • 打赏
  • 举报
回复
昨天去面试的。
只知道Oracle怎么取,不知道mssql怎么取,郁闷。
[Quote=引用 5 楼 Teng_s2000 的回复:]
面试题?哈哈
[/Quote]
gao512008 2009-04-02
  • 打赏
  • 举报
回复
Study
htl258_Tony 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 fengsky491 的帖子:]
有表(主键为自动增长列,但可能不连续),
问:
怎么查询指定行数的数据?
如,怎么取得21行到29行的数据?
[/Quote]

declare @t table(id int,col varchar(10))
insert @t select 1,'a'
insert @t select 2,'b'
insert @t select 3,'c'
insert @t select 4,'d'
insert @t select 5,'e'
insert @t select 6,'f'
insert @t select 7,'g'
insert @t select 8,'h'
insert @t select 12,'i'
insert @t select 13,'j'
insert @t select 15,'k'
insert @t select 16,'l'
insert @t select 17,'m'
insert @t select 18,'n'
insert @t select 19,'o'
insert @t select 20,'p'
insert @t select 21,'q'
insert @t select 22,'r'
insert @t select 23,'s'
insert @t select 24,'t'
insert @t select 25,'u'
insert @t select 26,'v'
insert @t select 27,'w'
insert @t select 28,'x'
insert @t select 29,'y'
insert @t select 30,'z'
insert @t select 31,'a'
insert @t select 32,'b'
insert @t select 33,'c'
insert @t select 34,'d'
insert @t select 35,'e'
insert @t select 36,'f'

select * from @t t where (select count(1) from @t where id<=t.id) between 21 and 29
/*
id col
----------- ----------
25 u
26 v
27 w
28 x
29 y
30 z
31 a
32 b
33 c

(9 行受影响)
*/

lihan6415151528 2009-04-01
  • 打赏
  • 举报
回复

select top n * from
(select top m * from tablename order by columnname asc) a
order by columnname desc


fan_xiaohu 2009-04-01
  • 打赏
  • 举报
回复
order by 后面是 IdentityId 而不是 Identity 写错了不好意思
fan_xiaohu 2009-04-01
  • 打赏
  • 举报
回复
假设自增字段名为IdentityId

select top 9 * from TableName where IdentityId not in (select top 20 * from TableName order by IdentityId asc) order by Identity asc
  • 打赏
  • 举报
回复
查询第X页,每页Y条记录

最基本的处理方法(原理):

如果表中有主键(记录不重复的字段也可以),可以用类似下面的方法,当然y,(x-1)*y要换成具体的数字,不能用变量:

select top y * from 表 where 主键 not in(select top (x-1)*y 主键 from 表)



如果表中无主键,可以用临时表,加标识字段解决.这里的x,y可以用变量.

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between (x-1)*y and x*y-1


ks_reny 2009-04-01
  • 打赏
  • 举报
回复

select top 9 * from
(select top 29 * from tablename order by columnname) a
order by columnname desc
Teng_s2000 2009-04-01
  • 打赏
  • 举报
回复
面试题?哈哈
play7788 2009-04-01
  • 打赏
  • 举报
回复
向小梁学习。
sdhdy 2009-04-01
  • 打赏
  • 举报
回复
select * from (
select *,px=(select count(1) from tb where 主键<a.主键)+1 from tb a) b
where px between 21 and 29
rucypli 2009-04-01
  • 打赏
  • 举报
回复
select *
from (
select *,row_number() over(order by idx) as row_num
from tb
)T
where row_num between 21 and 29
liangCK 2009-04-01
  • 打赏
  • 举报
回复
取n到m行

1.
select top m * from tablename where id not in (select top n id from tablename order by id asc/*|desc*/)

2.
select top m * into 临时表(或表变量) from tablename order by columnname -- 将top m笔插入到临时表
set rowcount n --只取n条结果
select * from 表变量 order by columnname desc

3.
select top n * from
(select top m * from tablename order by columnname) a
order by columnname desc


4.如果tablename里没有其他identity列,那么:
先生成一个序列,存储在一临时表中.
select identity(int) id0,* into #temp from tablename

取n到m条的语句为:
select * from #temp where id0 > =n and id0 <= m

如果你在执行select identity(int) id0,* into #temp from tablename这条语句的时候报错,那是因为你的DB中间的select into/bulkcopy属性没有打开要先执行:
exec sp_dboption 你的DB名字,'select into/bulkcopy',true


5.如果表里有identity属性,那么简单:
select * from tablename where identity_col between n and m

6.SQL2005开始.可以使用row_number() over()生成行号
;with cte as
(
select id0=row_number() over(order by id),* from tablename
)
select * from cte where id0 between n to m

34,588

社区成员

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

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