SqlServer中使用Mysql中的limit分頁功能


子查詢得到逆序排列的前PageIndex*PageSize條記錄,然后把這PageIndex*PageSize條記錄按正序排列得到前PageSize條記錄。OK了,這里的前PageSize條記錄,實際上就是我們想要的記錄。下面是一個具體的例子:
注:其中的PageSize(分頁的大小)和PageIndex(分頁索引,初始假設為0)均為變量,

方法1:
SELECT * FROM (SELECT TOP PageSize * FROM (SELECT TOP [PageIndex*PageSize] * FROM table ORDER BY id asc) as b ORDER BY id desc) as c ORDER BY id asc

 

方法2(推薦):

SELECT * from (SELECT row_number() over (order by id asc) as rownumber, * FROM tAvctoun WHERE t_world = 0  and tdate between '2012-11-1' and '2012-11-20') as t Where t.rownumber > 15 And t.rownumber <= 20

 

方法3(帶group by):

SELECT * FROM (SELECT row_number() over (order by convert(varchar(10),dDate,120) asc) as rownumber,count(nUserNo) as persons,convert(varchar(10),dDate,120) as dDate FROM tUser GROUP BY convert(varchar(10),dDate,120) HAVING convert(varchar(10),dDate,120) BETWEEN '2011-11-24' and '2011-11-27') as t where t.rownumber > 0 and t.rownumber <=5

(group by 中 用函數查出來的列需要指定別名 不然會報錯)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM