oracle 三层嵌套查询,oracle 三层嵌套分页查询
Oracle三層嵌套分頁查詢
--table A
select * from (select ta.*,rownum r? from (select * from A) ta where rownum < 10) where r > 5
--注: 其實兩層就可以,不過,兩層嵌套查詢不會用到oracle的外層條件內推機制,效率慢了點
rownum原理:
很簡單,rownum是oracle根據查詢動態生成額,記住,產生rownum之前不看where條件子句,所以當查詢一條結果時,rownum被oracle初始化為1,
這時執行where子句,成立則繼續,否則則丟棄,這也就是為什么rownum=1? rownum
rownum=2為什么不好使?
1 Oracle executes your query.
1.執行查詢操作
2 Oracle fetches the first row and calls it row number 1.
2.將第一行的row num置為1
3 Have we gotten past row number meets the criteria? If no, then Oracle discards the row, If yes, then Oracle return the row.
3.將得到的行的row num與條件相比較,如果不匹配,則拋棄行,如果匹配,則返回行
4 Oracle fetches the next row and advances the row number (to 2, and then to 3, and then to 4, and so forth).
4.oracle獲取下一行,然后將rownum增1
5 Go to step 3.
5.返回第3步
從這個原理可以知道,select rownum,name from emp where rownum>5;不返回行,因為首先執行查詢select name from emp,將第1行的rownum標為1,然后看where條件,為false,則拋棄行,執行第2行,還是rownum標為1,看where條件還為false,所以永遠是false,rownum不改變,所有的行都被拋棄,所以沒有結果。
總結
以上是生活随笔為你收集整理的oracle 三层嵌套查询,oracle 三层嵌套分页查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle里有limit怎么用,[OR
- 下一篇: oracle数据转成sqlserver,