国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫(kù) > MySQL > 正文

解析數(shù)據(jù)庫(kù)分頁的兩種方法對(duì)比(row_number()over()和top的對(duì)比)

2024-07-24 13:02:21
字體:
供稿:網(wǎng)友
今天,老師帶偶們復(fù)習(xí)了一下數(shù)據(jù)庫(kù)中的分頁,總體來說,今天感覺還不錯(cuò),因?yàn)橐郧皩W(xué)的還沒忘。好了,進(jìn)入正題,
首先,說說top的方法
top方法其實(shí)就是將你要查的的頁數(shù)的數(shù)據(jù)前得數(shù)據(jù)去掉 再取前幾
例:

復(fù)制代碼 代碼如下:


 一頁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)      --關(guān)鍵就在于not  in上 靠他來去掉前幾頁的數(shù)據(jù)
                    如果想要自己設(shè)定每頁幾條數(shù)據(jù)和看第幾頁的話也行 就多加個(gè)存儲(chǔ)過程
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


然后,我們?cè)僬f說ROW_NUMBER()over()的方法
這個(gè)其實(shí)就是又給數(shù)據(jù)表加了一個(gè)列在用來確定數(shù)據(jù)是第幾條
例:

復(fù)制代碼 代碼如下:


                       一頁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è)定每頁幾條數(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


恩 就這樣 這是我的理解 希望能給看得人帶來幫助吧~
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白水县| 从江县| 昌吉市| 牙克石市| 昭苏县| 日土县| 泰宁县| 离岛区| 吉林市| 青浦区| 平南县| 久治县| 绥阳县| 岢岚县| 赤城县| 泸水县| 诸城市| 任丘市| 鲁山县| 涡阳县| 台北市| 新蔡县| 原平市| 嵊泗县| 比如县| 古丈县| 吉木萨尔县| 贡嘎县| 平昌县| 延川县| 屯留县| 凤山市| 芦溪县| 牡丹江市| 鱼台县| 齐河县| 临桂县| 嘉峪关市| 常宁市| 离岛区| 娄底市|