SQL SERVER实现类似MySQL中的limit功能

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/marko_zheng/article/details/87279263

SQL SERVER实现类似MySQL中的limit功能

方法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 

方法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

同理 :查询从 第三条数据开始 往后数5条数据

declare @m int =3;
declare @n int = 5;

select top (@n) * from stu where id not in(
select top (@m-1) id from stu
)

猜你喜欢

转载自blog.csdn.net/marko_zheng/article/details/87279263