怎么設(shè)置MSSQL查詢數(shù)據(jù)分頁
發(fā)表時(shí)間:2023-07-18 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]這幾天剛好碰到數(shù)據(jù)的分頁查詢,覺得不錯(cuò),Mark一下,方法有兩種,都是使用select top,效率如何就不在這討論方法1:利用select top配合not in(或者not exists),查詢...
這幾天剛好碰到數(shù)據(jù)的分頁查詢,覺得不錯(cuò),Mark一下,方法有兩種,都是使用select top,效率如何就不在這討論
方法1:利用select top配合not in(或者not exists),查詢第n頁的時(shí)候,過濾掉n-1頁的數(shù)據(jù)即可,示例假設(shè)每頁查詢數(shù)量為5,查詢第3頁的數(shù)據(jù);
Select Top 5 UserCode,UserName from userInfo where UserCode not in (select top ((3-1)*5) UserCode from UserInfo order by UserCode asc) order by UserCode asc

前15行的數(shù)據(jù)

第三頁的數(shù)據(jù)
注意查詢的時(shí)候order by 必須使用相同的列及排列;
方法2:利用Row_Number()內(nèi)置函數(shù),先給查詢的表加上一列ID,然后查詢第幾頁就很簡單了 between ..and...
select UserCode,UserName,PassWord From
(Select UserCode,UserName,PassWord,Rn=Row_Number() OVER(order by UserCode desc) From UserInfo) AS T
Where t.Rn between (3-1)*5 and 3*5

當(dāng)然實(shí)際應(yīng)用中每頁記錄數(shù)量,查詢第幾頁都可以使用參數(shù)來代替。
以上就是如何操作MSSQL查詢數(shù)據(jù)分頁 的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
學(xué)習(xí)教程快速掌握從入門到精通的SQL知識。