Oracle中經典分頁代碼

來源:互聯網
上載者:User

Oracle中經典分頁代碼

在Oracle中因為沒有top關鍵字,所以在sqlserver中的分頁代碼並不適用於Oracle,那麼在Oracle中如何來實現分頁呢?

--查詢所有資料

STUNO  STUNAME                  STUAGE      STUID  STUSEAAT                                                                                                                                           
------ -------------------- ---------- ---------- ----------                                                                                                                                           
9      王五                        15 5.9876E+15          5                                                                                                                                           
13    哈哈                        15 5.9876E+15          5                                                                                                                                           
15    李四                        12 1.5666E+10          6                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5                                                                                                                                           
1      66                          10 5566554666          5         

比如說我要查詢stuInfo表中第二到第四條記錄

--兩層嵌套分頁

SQL> --兩層嵌套分頁
SQL> select * from (select stuInfo.*,rownum as rn from stuInfo where rownum<=4) where rn >=2;

STUNO  STUNAME                  STUAGE      STUID  STUSEAAT        RN                                                                                                                               
------ -------------------- ---------- ---------- ---------- ----------                                                                                                                               
13    哈哈                        15 5.9876E+15          5                                                                                                                                           
15    李四                        12 1.5666E+10          6                                                                                                                                           
1      66                          10 5566554666          5         

--如果我沒有對原始表有其他的排序操作的話,兩層嵌套就可以滿足需求了,但是如果我有一個條件是必須先將學號進行降序排列,然後取第二到第四條記錄呢?

那麼就必須使用三層嵌套了

SQL> select * from (select stu.* , rownum as rn from (select stuInfo.* from stuI
nfo order by stuno desc) stu where rownum<=4) where rn>=2;

STUNO  STUNAME                  STUAGE      STUID  STUSEAAT        RN
------ -------------------- ---------- ---------- ---------- ----------
15    李四                        12 1.5666E+10          6          2
13    哈哈                        15 5.9876E+15          5          3
1      66                          10 5566554666          5          4

這樣就完成了分頁查詢了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.