使用连接来代替in和not in(使用外连接技巧)
生活随笔
收集整理的這篇文章主要介紹了
使用连接来代替in和not in(使用外连接技巧)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
比如:表A里面的一個字段叫做MOBILE 里面存的記錄如下 : 12
3
4
5
6
7
8
1 表B里面的一個字段也叫做MOBILE里面存的記錄如下 1
2
3
4
1
9
10 (1)我們要查詢一下A和B里面都有的,以前我使用的是 select?A.mobile from? A where A.mobile in (select B.mobile from B) 出來結果為: 1
1
2
3
4 沒關系,去除重復就得到結果1,2,3,4了 現在我們使用另外一種SQL呢: select A.mobile from A,B where A.mobile=B.mobile 結果為 1
1
2
3
4
1
1 同樣濾重得到結果1,2,3,4 (2)第二個實驗我們要取一下在A中有的,而在B中沒有的,以前我都是使用not in 不要太熟練了,呵呵!不過從來也不考慮個效率。 select??A.mobile from? A where A.mobile not in (select B.mobile from B) 得出結果 5
6
7
8 然后我們再使用連接在處理 select A.mobile from A,B where A.mobile=B.mobile(+) and B.mobile is null 這條語句還可以表示為: select A.mobile from A left outer? join B on (A.mobile=B.mobile) where B.mobile is null 結果為: 6
5
7
8 (3) 第三個實現我們要取B中有的,而A中沒有的,直接用連接了 select B.mobile from B left outer join A on (B.mobile=A.mobile) where A.mobile is null 等價于 select B.mobile from A,B where A.mobile(+)=B.mobile and?A.mobile is null? 等價于 select B.mobile from A right outer join B on (A.mobile=b.mobile) where A.mobile is null 結果為: 10
9 這樣的話大家應該對左外連接,右外連接有個理解了吧!!!使用連接肯定是要比not in 的效率高多了,這可不是我說的DBA說的!呵呵!ORACLE10G測試通過! 超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的使用连接来代替in和not in(使用外连接技巧)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贪吃蛇系列之一——引入
- 下一篇: U-Mail邮件服务器树状通讯录实现智能