解析數(shù)據(jù)庫分頁的兩種方法對比(row_number()over()和top的對比)
更新時間:2013年07月02日 10:02:47 作者:
本篇文章是對數(shù)據(jù)庫分頁的兩種方法對比(row_number()over()和top的對比)進行了詳細的分析介紹,需要的朋友參考下
今天,老師帶偶們復習了一下數(shù)據(jù)庫中的分頁,總體來說,今天感覺還不錯,因為以前學的還沒忘。好了,進入正題,
首先,說說top的方法
top方法其實就是將你要查的的頁數(shù)的數(shù)據(jù)前得數(shù)據(jù)去掉 再取前幾
例:
一頁3條數(shù)據(jù) 取第一頁的數(shù)據(jù)
-- 第一頁
select top 3 * from T_news;
取第五頁的數(shù)據(jù)
--第五頁
select top 3 * from T_News where id not in (select top (3*4) id from T_News) --關鍵就在于not in上 靠他來去掉前幾頁的數(shù)據(jù)
如果想要自己設定每頁幾條數(shù)據(jù)和看第幾頁的話也行 就多加個存儲過程
create proc usp_fenye @geshu int,@yeshu int
as
begin
select top (@geshu) * from T_News where id not in (select top (@geshu*(@yeshu-1)) id from T_News)
end
然后,我們再說說ROW_NUMBER()over()的方法
這個其實就是又給數(shù)據(jù)表加了一個列在用來確定數(shù)據(jù)是第幾條
例:
一頁3條數(shù)據(jù) 取第一頁的數(shù)據(jù)
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between 1 and 3;
第五頁的數(shù)據(jù)
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between 3*4+1 and 3*5;
自己設定每頁幾條數(shù)據(jù)和看第幾頁
create proc usp_fenye @geshu int,@yeshu int
as
begin
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between @geshu*(@yeshu-1)+1 and @geshu*@yeshu;
end
恩 就這樣 這是我的理解 希望能給看得人帶來幫助吧~
首先,說說top的方法
top方法其實就是將你要查的的頁數(shù)的數(shù)據(jù)前得數(shù)據(jù)去掉 再取前幾
例:
復制代碼 代碼如下:
一頁3條數(shù)據(jù) 取第一頁的數(shù)據(jù)
-- 第一頁
select top 3 * from T_news;
取第五頁的數(shù)據(jù)
--第五頁
select top 3 * from T_News where id not in (select top (3*4) id from T_News) --關鍵就在于not in上 靠他來去掉前幾頁的數(shù)據(jù)
如果想要自己設定每頁幾條數(shù)據(jù)和看第幾頁的話也行 就多加個存儲過程
create proc usp_fenye @geshu int,@yeshu int
as
begin
select top (@geshu) * from T_News where id not in (select top (@geshu*(@yeshu-1)) id from T_News)
end
然后,我們再說說ROW_NUMBER()over()的方法
這個其實就是又給數(shù)據(jù)表加了一個列在用來確定數(shù)據(jù)是第幾條
例:
復制代碼 代碼如下:
一頁3條數(shù)據(jù) 取第一頁的數(shù)據(jù)
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between 1 and 3;
第五頁的數(shù)據(jù)
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between 3*4+1 and 3*5;
自己設定每頁幾條數(shù)據(jù)和看第幾頁
create proc usp_fenye @geshu int,@yeshu int
as
begin
select * from (select *,ROW_NUMBER()over(order by id asc) as number from T_News ) as tb1
where number between @geshu*(@yeshu-1)+1 and @geshu*@yeshu;
end
恩 就這樣 這是我的理解 希望能給看得人帶來幫助吧~
相關文章
解析mysql中max_connections與max_user_connections的區(qū)別
本篇文章是對mysql中max_connections與max_user_connections的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-06-06
解決Windows10下mysql5.5數(shù)據(jù)庫命令行中文亂碼問題
重置系統(tǒng)后,很久之前安裝的MySQL數(shù)據(jù)庫出現(xiàn)了控制臺查詢中文亂碼問題,時間太久早已經(jīng)不記得怎么設置了。下面通過本文給大家分享Windows10下解決MySQL5.5數(shù)據(jù)庫命令行中文亂碼問題,一起看看吧2017-07-07
MySQL優(yōu)化器追蹤(Optimizer Trace)的使用小結
MySQL OptimizerTrace 是用于分析查詢優(yōu)化器決策過程的工具,通過輸出JSON格式的詳細執(zhí)行信息,幫助開發(fā)者理解優(yōu)化器如何選擇執(zhí)行計劃,感興趣的可以了解一下2025-08-08

