Oracle、DB2、SQLSERVER、Mysql、Access分頁SQL語句梳理
最近把平時在項目中常用到的數(shù)據(jù)庫分頁sql總結(jié)了下。大家可以貼出分頁更高效的sql語句。
sqlserver分頁
第一種分頁方法
需用到的參數(shù):
pageSize 每頁顯示多少條數(shù)據(jù)
pageNumber 頁數(shù) 從客戶端傳來
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
pages 計算前pages 條數(shù)據(jù)
pages= pageSize*(pageNumber-1)
SQL語句:
select top pageSize * from 表名 where id not in (select top pages id from 表名 order by id) order by id
第二種分頁方法
pageSize 每頁顯示多少條數(shù)據(jù)
pageNumber 頁數(shù) 從客戶端傳來
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分頁
需用到的參數(shù):
pageSize 每頁顯示多少條數(shù)據(jù)
pageNumber 頁數(shù) 從客戶端傳來
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
pages 起始位置
pages= pageSize*(pageNumber-1)
SQL語句:
select * from 表名 limit pages, pageSize;
mysql 分頁依賴于關(guān)鍵字 limit 它需兩個參數(shù):起始位置和pageSize
起始位置=頁大小*(頁數(shù)-1)
起始位置=pageSize*(pageNumber -1)
oracle分頁
pageSize 每頁顯示多少條數(shù)據(jù)
pageNumber 頁數(shù) 從客戶端傳來
totalRecouds 表中的總記錄數(shù) select count (*) from 表名
totalPages 總頁數(shù)
totalPages=totalRecouds%pageSize==0?totalRecouds/pageSize:totalRecouds/pageSize+1
startPage 起始位置
startPage= pageSize*(pageNumber-1)+1
endPage=startPage+pageSize
SQL語句
select a.* from
(
select rownum num ,t.* from 表名 t where 某列=某值 order by id asc
)a
where a.num>=startPage and a.num<endPage
db2分頁
int startPage=1 //起始頁
int endPage; //終止頁
int pageSize=5; //頁大小
int pageNumber=1 //請求頁
startPage=(pageNumber-1)*pageSize+1
endPage=(startPage+pageSize);
SQL語句
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分頁
pageSize 每頁顯示多少條數(shù)據(jù)
pageNumber 頁數(shù) 從客戶端傳來
pages=pageSize*(pageNumber-1)+1
SQL語句
select top pageSize * from 表名 where id>=(select max(id) from (select top pages id from 表名 order by id asc ) t )