分頁查詢總結
oracle的分頁是先進行查詢並排序,而後在經過rownum進行範圍截取,而且要排序
內存分頁要本身對查詢結果進行排序,而後經過index進行範圍截取
重要的點是,首先要知道總的查詢結果數量totalNum,根據每頁大小pageSize,計算出總頁數totalPage,本頁結果數量curNum,
根據當前頁curPage和每頁大小pageSize獲得須要截取的範圍startIndex和endIndex
1.totalNum
2.pageSize、curPage
3.startIndex、endIndex
4.curNum
5.計算公式:
a.startIndex=pageSize*(curPage-1)+1
b.endIndex=startIndex + pageSize - 1
c.totalPage=(totalNum-1)/pageSize + 1sql
oracle分頁樣本:oracle
總數:app
ResultSet sets = stmt.executeQuery("select count(1) from ("+sql+")");
if(sets.next()){
totalNum = sets.getInt(1);
}spa
分頁結果:排序
StringBuffer buff = new StringBuffer(256);
int startIndex = pageSize * (curPage - 1) + 1;
int endIndex = startIndex + pageSize - 1;
buff.append("select * from (");
buff.append("select b.*,rownum row_num from (");
buff.append("select * from (" + sql + ") a order by " + orderBy);
buff.append(") b");
buff.append(") c where row_num between " + startIndex + " and "
+ endIndex + " order by " + orderBy);內存