Oracle、DB2、SQLSERVER、Mysql、Access分頁(yè)SQL語(yǔ)句梳理
溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫(xiě)此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!
最近把平時(shí)在項(xiàng)目中常用到的數(shù)據(jù)庫(kù)分頁(yè)sql總結(jié)了下。大家可以貼出分頁(yè)更高效的sql語(yǔ)句。
sqlserver分頁(yè)
第一種分頁(yè)方法
需用到的參數(shù):
pageSize 每頁(yè)顯示多少條數(shù)據(jù)
pageNumber 頁(yè)數(shù) 從客戶端傳來(lái)
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁(yè)數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
pages 計(jì)算前pages 條數(shù)據(jù)
pages= pageSize*(pageNumber-1)
SQL語(yǔ)句:
select top pageSize * from 表名 where id not in (select top pages id from 表名 order by id) order by id
第二種分頁(yè)方法
pageSize 每頁(yè)顯示多少條數(shù)據(jù)
pageNumber 頁(yè)數(shù) 從客戶端傳來(lái)
pages=pageSize*(pageNumber-1)+1
select top pageSize * from 表名 where id>=(select max(id) from (select top pages id from 表名 order by id asc ) t )
mysql分頁(yè)
需用到的參數(shù):
pageSize 每頁(yè)顯示多少條數(shù)據(jù)
pageNumber 頁(yè)數(shù) 從客戶端傳來(lái)
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁(yè)數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
pages 起始位置
pages= pageSize*(pageNumber-1)
SQL語(yǔ)句:
select * from 表名 limit pages, pageSize;
mysql 分頁(yè)依賴于關(guān)鍵字 limit 它需兩個(gè)參數(shù):起始位置和pageSize
起始位置=頁(yè)大小*(頁(yè)數(shù)-1)
起始位置=pageSize*(pageNumber -1)
oracle分頁(yè)
pageSize 每頁(yè)顯示多少條數(shù)據(jù)
pageNumber 頁(yè)數(shù) 從客戶端傳來(lái)
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁(yè)數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
startPage 起始位置
startPage= pageSize*(pageNumber-1)+1
endPage=startPage+pageSize
SQL語(yǔ)句
select a.* from
(
select rownum num ,t.* from 表名 t where 某列=某值 order by id asc
)a
where a.num>=startPage and a.num<endPage
db2分頁(yè)
int startPage=1 //起始頁(yè)
int endPage; //終止頁(yè)
int pageSize=5; //頁(yè)大小
int pageNumber=1 //請(qǐng)求頁(yè)
startPage=(pageNumber-1)*pageSize+1
endPage=(startPage+pageSize);
SQL語(yǔ)句
select * from (select 字段1,字段2,字段3,字段4,字段5,rownumber() over(order by 排序字段 asc ) as rowid from 表名 )as a where a.rowid >= startPage AND a.rowid <endPage
access分頁(yè)
pageSize 每頁(yè)顯示多少條數(shù)據(jù)
pageNumber 頁(yè)數(shù) 從客戶端傳來(lái)
pages=pageSize*(pageNumber-1)+1
SQL語(yǔ)句
select top pageSize * from 表名 where id>=(select max(id) from (select top pages id from 表名 order by id asc ) t )
溫馨提示:您的每一次轉(zhuǎn)載,體現(xiàn)了我寫(xiě)此文的意義!!!煩請(qǐng)您在轉(zhuǎn)載時(shí)注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!