在oracle中沒有top函數,不能像MySQL或者是SQLserver那樣進行分頁查詢,但是也提供了其它的方式,比如說rownum,rownum的作用和top類似,是行標記數。
創建一張表:A
CREATE TABLE A( ID INT, NAME VARCHAR2(20) );
並且插入一些數據
分頁查詢也有幾種方式:
(1)不排序分頁查詢,效率高。
例如:
查詢表A中的數據,且規定每一頁5條數據,那么查詢第二頁的分頁查詢SQL如下:
select * from (select rownum as rowno,tmp.* from A tmp where rownum<=2*5) B where b.rowno>5*(2-1);
(2)帶有排序的分頁查詢,效率較不排序要低
在和1同意的條件下,但是查詢的數據按照ID進行升序排序。
思路分析:
1、先排序
select * from a order by id asc;
2、再進行分頁查詢
select * from (select rownum as rowno,t.* from (select * from a order by id asc) t where ROWNUM<=5*2)S where s.rowno>5*(2-1);