Oracle实现分页时,需要引入一个rownum的函数,rownum可以给记录一个类似于id的字段。
以下收整理了常用的几种sql分页算法,数据库以Oracle中emp为例。查询结果如下:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980/12/17 800.00 20
7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981/4/2 2975.00 20
7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981/5/1 2850.00 30
7782 CLARK MANAGER 7839 1981/6/9 2450.00 10
7788 SCOTT ANALYST 7566 1987/4/19 3000.00 20
7839 KING PRESIDENT 1981/11/17 5000.00 10
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7900 JAMES CLERK 7698 1981/12/3 950.00 30
7902 FORD ANALYST 7566 1981/12/3 3000.00 20
7934 MILLER CLERK 7782 1982/1/23 1300.00 10
14 rows selected
分页方法1:使用嵌套查询
SQL> select * from ( select t1.* , rownum as n from emp t1 where rownum <= 15 ) where n >= 10;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO N
----- ---------- --------- ----- ----------- --------- --------- ------ ----------
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30 10
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20 11
7900 JAMES CLERK 7698 1981/12/3 950.00 30 12
7902 FORD ANALYST 7566 1981/12/3 3000.00 20 13
7934 MILLER CLERK 7782 1982/1/23 1300.00 10 14
分页方法2:使用minus
SQL> select * from emp where rownum <= 14 minus select * from emp where rownum < 10;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7900 JAMES CLERK 7698 1981/12/3 950.00 30
7902 FORD ANALYST 7566 1981/12/3 3000.00 20
7934 MILLER CLERK 7782 1982/1/23 1300.00 10
在方法二中,如果minus后的查询语句执行where rownum > 10时,为什么不能查询到任何的记录呢?
ROWNUM是对结果集加的一个伪列,即先查到结果集之后再加上去的一个列(强调:先有结果集后再添加伪列)。
简单的说 rownum 是对符合条件结果的序列号。它总是从1开始排起的。不能对rownum用">"。它与rownum实现的机制是有关的。同时, 也无法使用rownum = n(n>1的自然数)。rownum都是从1开始。
分页方法3 : 使用rowid
SQL>
select * from emp where rowid in ( select rid from (select rowid as
rid, rownum as id from emp t where rownum < 15) where id >= 10 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30
7900 JAMES CLERK 7698 1981/12/3 950.00 30
7902 FORD ANALYST 7566 1981/12/3 3000.00 20
7934 MILLER CLERK 7782 1982/1/23 1300.00 10
方法四:使用分析函数
SQL> select * from ( select t.* , row_number() over(order by ename) as num from emp t ) where num between 10 and 15;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO NUM
----- ---------- --------- ----- ----------- --------- --------- ------ ----------
7934 MILLER CLERK 7782 1982/1/23 1300.00 10 10
7788 SCOTT ANALYST 7566 1987/4/19 3000.00 20 11
7369 SMITH CLERK 7902 1980/12/17 800.00 20 12
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30 13
7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30 14
方法五:使用rownum伪列查询所有记录后,外层使用between and 或 大于 小于
SQL> select * from (select t.* , rownum as num from emp t) where num between 10 and 15;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO NUM
----- ---------- --------- ----- ----------- --------- --------- ------ ----------
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30 10
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20 11
7900 JAMES CLERK 7698 1981/12/3 950.00 30 12
7902 FORD ANALYST 7566 1981/12/3 3000.00 20 13
7934 MILLER CLERK 7782 1982/1/23 1300.00 10 14