oracle 三层嵌套查询,oracle 三层嵌套分页查询
Oracle三層嵌套分頁查詢
--table A
select * from (select ta.*,rownum r? from (select * from A) ta where rownum < 10) where r > 5
--注: 其實(shí)兩層就可以,不過,兩層嵌套查詢不會(huì)用到oracle的外層條件內(nèi)推機(jī)制,效率慢了點(diǎn)
rownum原理:
很簡(jiǎn)單,rownum是oracle根據(jù)查詢動(dòng)態(tài)生成額,記住,產(chǎn)生rownum之前不看where條件子句,所以當(dāng)查詢一條結(jié)果時(shí),rownum被oracle初始化為1,
這時(shí)執(zhí)行where子句,成立則繼續(xù),否則則丟棄,這也就是為什么rownum=1? rownum
rownum=2為什么不好使?
1 Oracle executes your query.
1.執(zhí)行查詢操作
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步
從這個(gè)原理可以知道,select rownum,name from emp where rownum>5;不返回行,因?yàn)槭紫葓?zhí)行查詢select name from emp,將第1行的rownum標(biāo)為1,然后看where條件,為false,則拋棄行,執(zhí)行第2行,還是rownum標(biāo)為1,看where條件還為false,所以永遠(yuǎn)是false,rownum不改變,所有的行都被拋棄,所以沒有結(jié)果。
總結(jié)
以上是生活随笔為你收集整理的oracle 三层嵌套查询,oracle 三层嵌套分页查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle里有limit怎么用,[OR
- 下一篇: c语言文件加密异或操作,用异或算法实现文