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

SQLServer常用分頁方式

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

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

0.原來的SQL的所有數(shù)據

 

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

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

 (1)取第一頁數(shù)據

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

 結果:

 

(2)取第二頁數(shù)據

  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 當前頁數(shù)-1*頁大小+1  and 頁數(shù)*頁大小   ,而且包含起始值與結束值。

 

補充:這種方式的通用寫法如下:   原來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是一款針對中小制造業(yè)的專業(yè)生產管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業(yè)務管理,結合碼頭的業(yè)務特點,圍繞調度、堆場作業(yè)而開發(fā)的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
羞羞影院午夜男女爽爽在线观看 | 在线观看人成视频色9 | 亚洲jizzjizz妇女 | 亚洲精品国产精品乱码在线 | 亚洲欧美日韩在线综合久 | 亚洲中文字字幕精品 |