码迷,mamicode.com
首页 > 数据库 > 详细

Oracle的分页查询

时间:2016-07-25 15:54:34      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

--1:无ORDER BY排序的写法。(效率最高)
--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!)

SELECT *
  FROM (SELECT ROWNUM AS rowno, t.*
          FROM emp t
         WHERE hire_date BETWEEN TO_DATE (20060501, yyyymmdd)
                             AND TO_DATE (20060731, yyyymmdd)
           AND ROWNUM <= 20) table_alias
 WHERE table_alias.rowno >= 10;

--2:有ORDER BY排序的写法。(效率最高)
--(经过测试,此方法随着查询范围的扩大,速度也会越来越慢哦!)

SELECT *
  FROM (SELECT tt.*, ROWNUM AS rowno
          FROM (  SELECT t.*
                    FROM emp t
                   WHERE hire_date BETWEEN TO_DATE (20060501, yyyymmdd)
                                       AND TO_DATE (20060731, yyyymmdd)
                ORDER BY create_time DESC, emp_no) tt
         WHERE ROWNUM <= 20) table_alias
 WHERE table_alias.rowno >= 10;

--3:无ORDER BY排序的写法。(建议使用方法1代替)
--(此方法随着查询数据量的扩张,速度会越来越慢哦!)

SELECT *
  FROM (SELECT ROWNUM AS rowno, t.*
          FROM k_task t
         WHERE flight_date BETWEEN TO_DATE (20060501, yyyymmdd)
                               AND TO_DATE (20060731, yyyymmdd)) table_alias
 WHERE table_alias.rowno <= 20 AND table_alias.rowno >= 10;

--TABLE_ALIAS.ROWNO  between 10 and 100;

 

--4:有ORDER BY排序的写法.(建议使用方法2代替)
--(此方法随着查询范围的扩大,速度会越来越慢哦!)

 

SELECT *
  FROM (SELECT tt.*, ROWNUM AS rowno
          FROM (  SELECT *
                  FROM k_task t
                   WHERE flight_date BETWEEN TO_DATE (20060501, yyyymmdd)
                                         AND TO_DATE (20060531, yyyymmdd)
                ORDER BY fact_up_time, flight_no) tt) table_alias
 WHERE table_alias.rowno BETWEEN 10 AND 20;

--5另类语法。(有ORDER BY写法)
--(语法风格与传统的SQL语法不同,不方便阅读与理解,为规范与统一标准,不推荐使用。)

WITH partdata AS
     (
        SELECT ROWNUM AS rowno, tt.*
          FROM (  SELECT *
                    FROM k_task t
                   WHERE flight_date BETWEEN TO_DATE (20060501, yyyymmdd)
                                         AND TO_DATE (20060531, yyyymmdd)
               ORDER BY fact_up_time, flight_no) tt WHERE ROWNUM <= 20)
SELECT *  FROM partdata WHERE rowno >= 10;

--6另类语法 。(无ORDER BY写法)

 

WITH partdata AS
     (
        SELECT ROWNUM AS rowno, t.*
          FROM k_task t
         WHERE flight_date BETWEEN TO_DATE (20060501, yyyymmdd)
                              AND TO_DATE (20060531, yyyymmdd)
           AND ROWNUM <= 20)
SELECT *  FROM partdata WHERE rowno >= 10;

 

 

 

 

Oracle的分页查询

标签:

原文地址:http://www.cnblogs.com/tingbogiu/p/5703529.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!