怎么从sqlserver数据库中提取从第m条到第n条记录

ydhj 2005-02-03 11:27:50
怎么从sqlserver数据库中提取从第m条到第n条记录?
...全文
867 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
coogool 2005-02-13
  • 打赏
  • 举报
回复
上面说可以用:
select * from table where col between m and n
我记得oracle中是可以的,但在sql-server中我试了一下,报错,请教什么原因?
建军黄 2005-02-06
  • 打赏
  • 举报
回复
假如m、n就是表中的ID号
select top (n-m) * from (select top n * from 表 order by id desc) order by id asc
wukele 2005-02-06
  • 打赏
  • 举报
回复
问一下
Jackile(快呀,快来呀!) 发表的
--处理查询的 topicid
set @i=@currentpage*@pagesize
set rowcount @i
select topicid into #t from tb_club_topic
where boardID=12402 And deletemark=0
order by istop desc, LastDateTime desc

set @i=@i-@pagesize ****
set rowcount @i
delete from #t

--返回查询结果
中的为什么要写set @i=@i-@pagesize;
好象可以写啊
ken2002 2005-02-04
  • 打赏
  • 举报
回复
从publish 表中取出第 n 条到第 m 条的记录:
SELECT TOP m-n+1 *
FROM publish
WHERE (id NOT IN
(SELECT TOP n-1 id
FROM publish))

id 为publish 表的关键字
Qihua_wu 2005-02-04
  • 打赏
  • 举报
回复
若你的表中存在identity字段可直接
select * from tablename where id>=m and id<=n

若不存在,可按下述办法完成
alter table tablename add id int identity(1,1)

select * from tablename where id>=10 and id<=20

alter table tablename drop id
ysm0_o 2005-02-03
  • 打赏
  • 举报
回复
select * from table where col between m and n
qxg1123 2005-02-03
  • 打赏
  • 举报
回复
从M到N以什么为准,换种排序方法呢?
xluzhong 2005-02-03
  • 打赏
  • 举报
回复
参考:

http://blog.csdn.net/xluzhong/articles/277598.aspx
islion 2005-02-03
  • 打赏
  • 举报
回复
用"Between and"
LIHY70 2005-02-03
  • 打赏
  • 举报
回复
xuexi
hr88rong 2005-02-03
  • 打赏
  • 举报
回复
有关键字的时候:
首先定义一个数num,num=n-m
SELECT TOP num *
FROM Balcony
WHERE (关键字 NOT IN
(SELECT TOP m 关键字
FROM Balcony))
如果没有关键字,就建立临时表,这我没试过!你可以照swand(钢板日穿)和ysm0_o(snoopy)说的试一下!
淡蓝冰 2005-02-03
  • 打赏
  • 举报
回复
取出从第n1条到第n2条中的记录

如果有关键:
select * from (select top n2 * from 表 ) a
where 关键字段 not in(select top n1 关键字段 from 表)

如果沒有,可以用临时表,加标识字段解决

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between n1 and n2

此方法是可以﹐我用過﹐我也想出來過﹐但是﹐實際中也會遇到很多的麻煩的。
光這樣是不行的。你最多只能運行一次﹐第二次就會出錯呢﹖因為它在SQL中建立了一個#tb表﹐你第二次運行時就會說此表已存在。所以必須每次運行后還要接著把它給刪除﹐下次運行時才不會出錯。
dzhfly 2005-02-03
  • 打赏
  • 举报
回复
支持Mryu666() 的顶
rui520_2002 2005-02-03
  • 打赏
  • 举报
回复
邹建大哥写的sql就是不一样
phylory 2005-02-03
  • 打赏
  • 举报
回复
大家都是牛人啊
最近很收益
很赞同swand(钢板日穿) 的方法,
简单,易懂
Jackile 2005-02-03
  • 打赏
  • 举报
回复
怎么从sqlserver数据库中提取从第m条到第n条记录?


1 写个存储过程
2 给你一段代码参考下,邹建写的
create proc p_qry
@currentpage int,
@pagesize int=20 --默认每页20条记录
as
set nocount on

--查询处理
if @currentpage=1 --第一页就直接查询
begin
set rowcount @pagesize ******
select * from tb_club_topic
where boardID=12402 And deletemark=0
order by istop desc, LastDateTime desc
end
else
begin --如果查询的不是第一页
declare @i int

--处理查询的 topicid
set @i=@currentpage*@pagesize
set rowcount @i
select topicid into #t from tb_club_topic
where boardID=12402 And deletemark=0
order by istop desc, LastDateTime desc

set @i=@i-@pagesize
set rowcount @i
delete from #t

--返回查询结果
select * from tb_club_topic a
where exists(
select * from #t where topicid=a.topicid)
order by istop desc, LastDateTime desc
end
set rowcount 0
go

--调用实现分页查询
exec p_qry @currentpage=2

---------------------------------------------------------------
---------------------------------------------------------------
数据测试,表数据为94万,最多的一个board为47万纪录 结果如下

select top 方法:
页数 纪录数 执行时间(ms)
5页 100 0-30
50页 1000 400ms
500页 10000 等了很长时间没有结果
1000页 20000 1200 (可能因为我增加了纪录数的原因,和上午的结果已经不一样了,not in的效率与纪录数以及选出的结果数有很大关系)
>2000 >40000 >3000 (选取结果数很大时,基本上select top 方法都要较长的时间,但相差不多,维持在3000-7000之内)


临时表方法:
页数 纪录数 执行时间(ms)
5页 100 30 (在页数很小时,因为临时表要执行额外的操作,所以会比select top 慢一丁点)
1000页 20000 300
2000页 40000 500
3000页 60000 700
.... ... ...
10000页 100000 2400 (基本上执行时间随页数增长,每增长1000页20000条纪录增长200ms),不受符合条件总纪录数限制,即纪录数多的board和纪录数小的board执行时间相差不大。


M-N条: 拆分为0-N 出去0-M条 。关键行代码给你打上*.
swand 2005-02-03
  • 打赏
  • 举报
回复

取出从第n1条到第n2条中的记录

如果有关键:
select * from (select top n2 * from 表 ) a
where 关键字段 not in(select top n1 关键字段 from 表)

如果沒有,可以用临时表,加标识字段解决

select id=identity(int,1,1),* into #tb from 表
select * from #tb where id between n1 and n2
Mryu666 2005-02-03
  • 打赏
  • 举报
回复
begin set nocount on
declare @p1 int
exec sp_cursoropen @p1 output,'select * from table ' --把你要查询的语句写到里面也可以排序
exec sp_cursorfetch @p1,16,开始行数,结束行数 --把你想要查出的行数范围写上
exec sp_cursorclose @p1
end
Mryu666 2005-02-03
  • 打赏
  • 举报
回复
begin set nocount on
declare @p1 int
exec sp_cursoropen @p1 output,'select * from table ' --把你要查询的语句写到里面也可以排序
exec sp_cursorfetch @p1,16,1,10
exec sp_cursorclose @p1
end

34,576

社区成员

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

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