ORACLE分頁查詢SQL文法 分類: ORACLE專欄 2007-08-26 23:56 4850人閱讀 評論(1) 收藏 舉報
--1:無ORDER BY排序的寫法。(效率最高)
--(經過測試,此方法成本最低,只嵌套一層,速度最快!即使查詢的資料量再大,也幾乎不受影響,速度依然!)
SELECT *
FROM (Select ROWNUM AS ROWNO, T.*
from k_task T
where Flight_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 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) 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;