怎样查询第100~200行的数据?
这条语句只能查询前一百行的数据:
Select Top 100 * from table1
但要查询中间的某一段应该怎么查?
问题点数:100、回复次数:11Top
1 楼zjcxc(邹建)回复于 2004-09-03 10:58:03 得分 10
查询第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
Top
2 楼pbsql(风云)回复于 2004-09-03 10:58:32 得分 10
Select Top 100 * from table1
where id not in(Select Top 100 id from table1)Top
3 楼viptiger(六嘎)回复于 2004-09-03 10:59:05 得分 10
select top 100 *
from (select top 200 * from YouTable order by colX asc)
order by colx deseTop
4 楼viptiger(六嘎)回复于 2004-09-03 11:00:15 得分 10
--刚刚的有错别字 :)
select top 100 *
from (select top 200 * from YouTable order by colX asc) as a
order by colx descTop
5 楼haoK(haoK.Y)回复于 2004-09-03 11:00:27 得分 10
SELECT TOP 100 *
FROM yourTable
WHERE 主键 NOT IN (
SELECT TOP 100 主键
FROM yourTable
ORDER BY 排序列
)
ORDER BY 排序列
Top
6 楼LoveSQL(努力奋斗ing)回复于 2004-09-03 11:00:41 得分 10
Select identity(int,1,1) as sid,* into #tmp from table1
select * from #tmp where sid between 100 and 200
Top
7 楼cenphoenix(火凤凰)回复于 2004-09-07 11:07:24 得分 10
markTop
8 楼zlj113(·米老鼠· 学习)回复于 2004-09-07 14:26:03 得分 10
给你个思路:
例如查300-500之间的记录
先按升序查出500条记录,然后再在这个记录集里按降序查出200(500-300)条记录就OK了。
Top
9 楼torrent2008(微笑)回复于 2004-09-07 14:33:25 得分 0
study!Top
10 楼zndavid(穷书生)回复于 2004-09-07 17:00:03 得分 10
用临时表
Select identity(int,1,1) as sid,* into #tmp from table1
select * from #tmp where sid between 100 and 200Top
11 楼mousean(快乐无限)回复于 2004-09-07 17:10:22 得分 10
如果原表中有identity标志的字段,楼上的招就不灵了Top




