原文地址:https://blog.youkuaiyun.com/marko_zheng/article/details/87279263
方法1 :
top 和order by 实现,当数据表庞大的时候开小会很大
--查询@m 到@n 条数据
declare @m int;
declare @n int;
--- select top @m * from (select top @n * from stu order by id desc) as a -- 语法错误
select top (@m) * from (select top (@n) * from stu order by id desc) as a
方法2:
利用top 和唯一主键
--查询第3条 到 第8条
declare @m int =3;
declare @n int = 8;
select top (@n-@m+1) * from stu where id not in (
select top (@m-1) id from stu
忽略前 m 条数据,从第 (m+1)条数据开始(包括m+1条在内) 往后取n条数据
declare @m int =3;
declare @n int = 5;
select top (@n) * from stu where id not in(
select top (@m) id from stu
)
本文介绍了两种在SQL中进行高效分页查询的方法。一种是使用TOP和ORDER BY结合唯一主键,另一种则是利用TOP和不在子查询中出现的ID。这两种方法能够帮助开发者在面对大量数据时,有效地进行分页查询,提高查询效率。
1325

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



