sql多表分頁
發布時間: 2023-06-11 23:32:56
⑴ 如何用sql語句 實現分頁查詢
適用於 SQL Server 2000/2005
SELECT TOP 頁大小 *
FROM table1
WHERE id NOT IN
SELECT TOP 頁大小*(頁數-1) id FROM table1 ORDER BY id
⑵ 幾種常見SQL分頁方式
createtablepagetest
(
idintidentity(1,1)notnull,
col01intnull,
col02nvarchar(50)null,
col03datetimenull
)
--分頁1,notin/top
selecttop50*frompagetest
whereidnotin()
orderbyid
--分頁2,notexists
selecttop50*frompagetest
wherenotexists
(select1from()awherea.id=pagetest.id)
orderbyid
--寫法3,max/top
selecttop50*frompagetest
whereid>(selectmax(id)from()a)
orderbyid
--分頁4,row_number()
selecttop50*from
(selectrow_number()over(orderbyid)rownumber,*frompagetest)a
whererownumber>9900
select*from
(selectrow_number()over(orderbyid)rownumber,*frompagetest)a
whererownumber>9900andrownumber<9951
select*from
(selectrow_number()over(orderbyid)rownumber,*frompagetest)a
--分頁5,在csdn上一帖子看到的,row_number()變體,不基於已有欄位產生記錄序號,先按條件篩選以及排好序,再在結果集上給一常量列用於產生記錄序號
select*
from(
selectrow_number()over(orderbytempColumn)rownumber,*
from(selecttop9950tempColumn=0,*frompagetestwhere1=1orderbyid)a
)b
whererownumber>9900
結論:
1.max/top,ROW_NUMBER()都是比較不錯的分頁方法。相比ROW_NUMBER()只支持sql2005及以上版本,max/top有更好的可移植性,能同時適用於sql2000,access。
2.not exists感覺是要比not in效率高一點點。
3.ROW_NUMBER()的3種不同寫法效率看起來差不多。
4.ROW_NUMBER() 的變體基於這個測試效率實在不好。
⑶ 對於多表關聯的,大數據分頁,怎麼整sql
SELECT*
FROM(SELECT查詢欄位,
ROW_NUMBER()OVER(ORDERBY排序欄位)ASNum
FROM表1a
INNERJOIN表2bONa.關聯欄位=b.關聯欄位
)t
WHEREt.NumBETWEEN10AND20
⑷ sql多表關聯,數據量比較大的分頁查詢,怎麼做,有沒有較好的方法,
可以做一個存儲過程,傳入參數的方式,參數可以有顯示第幾頁,分頁大小,可以返回總行數和查詢結果
⑸ 如何使用sql語句進行分頁操作
sql語句是不能進行分頁操作的,它只查出一個相應條件的數據記錄集。而分頁操作,是由程序員用相應的軟體開發語言設計演算法,而過進行分頁操作。謝謝
熱點內容