本帖最后由 刘泽宇 于 2023-12-3 12:37 编辑
第一种:ROW_NUMBER() OVER()方式
select * from (
select *, ROW_NUMBER() OVER(Order by ArtistId ) AS RowId from ArtistModels
) as b
where RowId between 10 and 20
---where RowId BETWEEN 当前页数-1*条数 and 页数*条数---
执行结果是:
file:///C:\Users\lzy\AppData\Roaming\Tencent\Users\1255838161\QQ\WinTemp\RichOle\MLWZP40MX)HHT%T1N4EYVUI.png
第二种方式:offset fetch next方式(SQL2012以上的版本才支持:推荐使用 )
select * from ArtistModels order by ArtistId offset 4 rows fetch next 5 rows only
--order by ArtistId offset 页数 rows fetch next 条数 rows only ----
执行结果是:
file:///C:\Users\lzy\AppData\Roaming\Tencent\Users\1255838161\QQ\WinTemp\RichOle\QACCR_)FQ}DRA_GSH7EHK{E.png
第三种方式:--top not in方式 (适应于数据库2012以下的版本)
select top 3 * from ArtistModels
where ArtistId not in (select top 15 ArtistId from ArtistModels)
------where Id not in (select top 条数*页数 ArtistId from ArtistModels)
执行结果:
file:///C:\Users\lzy\AppData\Roaming\Tencent\Users\1255838161\QQ\WinTemp\RichOle\KRA4}6OLUP(5M65(M1KB`5Q.png
第四种方式:用存储过程的方式进行分页
CREATE procedure page_Demo
@tablename varchar(20),
@pageSize int,
@page int
AS
declare @newspage int,
@res varchar(100)
begin
set @newspage=@pageSize*(@page - 1)
set @res='select * from ' +@tablename+ ' order by ArtistId offset '+CAST(@newspage as varchar(10)) +' rows fetch next '+ CAST(@pageSize as varchar(10)) +' rows only'
exec(@res)
end
EXEC page_Demo @tablename='ArtistModels',@pageSize=3,@page=5
执行结果:
file:///C:\Users\lzy\AppData\Roaming\Tencent\Users\1255838161\QQ\WinTemp\RichOle\L``~2E9S`%1DOCRV$9A`XMQ.png
|