mysql 、pgsql、oracle 常見分頁
阿新 • • 發佈:2018-11-07
// 總頁數 int pages=0; // 查詢總行數的SQL String rowSql= "select count(*) from "+ table_name; ...... // 判斷頁數,如果是頁大小的整數倍就為rows/pageSize如果不是整數倍就為rows/pageSize+1 if (rows % pageSize == 0) { pages = rows / pageSize; } else { pages = rows / pageSize + 1; }
start :開始行數 currentPageNo:當前頁碼 pagesize:每頁條數
1,mysql: mysql start 從0開始
select * from table_name limit start ,pagesize -- 傳遞 currentPageNo(頁碼)、pageSize(每頁條數) select * from table limit (currentPageNo-1)*pageSize,pageSize; --當數量到達萬級以上,利用主鍵或者唯一索引進行資料分頁,提高效能 select * from table where _id > (currentPageNo-1)*pageSize limit pageSize;-- 同上並加上條件對資料排序返回 select * from table where _id > (currentPageNo-1)*pageSize order by _id limit pageSize;
2,pgsql : start 從0開始
select * from table_name limit pageSize offset start; -- 當start為0可以簡寫如下 select * from table_name limit pageSize --排序 select * from table_name orderby xxxx limit pageSize offset start; --加當前頁碼currentPageNo select * from xxx limit pagesize offset (currentPageNo-1)*pagesize;
3,Oracle
--在Oracle中實現分頁的方法大致分為兩種,用ROWNUM關鍵字和用ROWID關鍵字 --ROWNUM select * from ( select rownum as row_no, t.* from table_name t where xxx='xxx' and rownum <= pageSize*currentPageNo ) table_alias where table_alias.row_no > pageSize*(currentPageNo -1); --ROWID SELECT * FROM (SELECT RID FROM (SELECT R.RID, ROWNUM LINENUM FROM (SELECT ROWID RID FROM TABLE1 WHERE TABLE1_ID = XX ORDER BY order_date DESC) R WHERE ROWNUM <= 20) WHERE LINENUM >= 10) T1, TABLE1 T2 WHERE T1.RID = T2.ROWID;