从第N条开始取M条
select top M * from tables where 主键 not in (select top N 主键 from tables)
从第N条取到第M条
select top M-N * from tables where 主键 not in (select top N 主键 from tables)
要求表要有主键,M>N
另还有一种:将结果集放入一个临时表中(#tmp),在建临时表时给其加一个自增字段.
然后再通过这个自增字段来选取所需记录.
select identity(int,0,1) as tmpid,* into #tmp from table where ......
identity(int,0,1) as tmpid:做一个自增数,类型为Int,从0开始,增量1.别名为tmpid.
select * into #tmptable from table:将此次select的结果放入一个临时表#tmptable中.
从第N条取到第M条
select * from #tmp where tmpid>=N and tmpid<=M;
从第N条开始取M条
select * from #tmp where tmpid>=M and tmpid<=N+M;
本文介绍了使用SQL进行分页查询的几种方法,包括利用主键排除已获取的数据、创建临时表并通过自增字段选取特定范围的记录等。这些技巧对于优化数据库性能、实现高效的数据检索非常实用。
1091

被折叠的 条评论
为什么被折叠?



