LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

SQLServer常用分頁方式

freeflydom
2025年5月9日 9:27 本文熱度 1283

  mysql的分頁是基于limit關鍵字,oracle的分頁是基于rownum行號,SQLserver的分頁在下面進行研究,是基于SQLServer2012進行的測試。

0.原來的SQL的所有數據

 

下面的測試假設每頁都是取5條數據。

1.第一種-ROW_NUMBER() OVER()方式(over函數必須有)

 (1)取第一頁數據

  select * from ( 
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user] 
  ) as b
      where RowId between 1 and 5;

 結果:

 

(2)取第二頁數據

  select * from ( 
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user] 
  ) as b
      where RowId between 6 and 10;

結果:

 

 總結:  這種方式采用    RowId BETWEEN 當前頁數-1*頁大小+1  and 頁數*頁大小   ,而且包含起始值與結束值。

 

補充:這種方式的通用寫法如下:   原來SQL不能帶order by ,但是可以帶條件。

原來SQL =     select * from [mydb].[dbo].[user] where name like 'name%'    

拼接分頁的模板如下: 

 select * from ( 
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from    (
            原來SQL
        ) AS A
) as B
where RowId between 1 and 5;

 

 

 

2.第二種-offset start fetch next page rows only

(1)取第一頁

select * from [mydb].[dbo].[user]   order by ID offset 0 rows fetch next 5 rows only;

結果:

 

 

(2)取第二頁

select * from [mydb].[dbo].[user]   order by ID offset 5 rows fetch next 5 rows only;

結果:

 

 總結:這種方式的起始值與結束值計算方式: offset 頁號*頁大小 rows fetch next 頁大小 rows only  

 

3.第三種: top 關鍵字

 (1)取第一頁

select top 5 * from [mydb].[dbo].[user] 
where ID not in (select top 0 ID from [mydb].[dbo].[user]);

結果:

 

(2)取第二頁

select top 5 * from [mydb].[dbo].[user] 
where ID not in (select top 5 ID from [mydb].[dbo].[user]);

結果:

 

  總結:這種方式只用改內層的 top就可以了:  內層的top后面相當于起始值,計算方式為  (頁號-1)*頁大小。

  補充:這種分頁方式的通用模板如下:  這個可以加order by和條件

原來SQL = select * from [mydb].[dbo].[user] where name like 'name%'   

select top 5 * from ( 
    原來SQL
) AS A where ID not in (select top 5 ID from [mydb].[dbo].[user]);

 

 

4.  ROW_NUMBER() + top 相當于上面1和3的結合使用

 (1)取第一頁

select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>0;

結果:

(2)取第二頁

select top (5) * from (select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from [mydb].[dbo].[user]) as A where A.RowId>5;

結果:

 

  總結:這種方式比較通用, 第一個 top 里面的值 相當于 頁大小,第二個rowID>起始值,起始值計算方式為  (頁號-1)*頁大小

補充:這種分頁方式的通用模板如下:    這種方式原來的SQL也不用加排序語句

原來SQL = select * from [mydb].[dbo].[user] where name like 'name%'   

select top (5) * from (
    select *, ROW_NUMBER() OVER(Order by ID ) AS RowId from (
        原來SQL
        ) as A   
) as B where B.RowId>5;

 

 

注意:文中SQLServer的AS A這些起別名不能省略。

?轉自https://www.cnblogs.com/qlqwjy/p/10305188.html


該文章在 2025/5/9 9:27:00 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
综合久久久久久久 | 一区二区三区AV大片 | 在线精品视频一区二区三四 | 日韩午夜精品一区二区三区 | 亚洲欧美久久夜夜综合伊人 | 亚洲欧美日韩高清在线播放 |