Oracle -- left join 什麼情況可以直接改成join
前言:
一般開發人員都比較喜歡寫left join,那是否有些情況不用根據業務情況就能將left join 改成join 呢?
情景① A left join B on a.a=b.a where b.任意欄位 =(只要不是is null) 可將left 去掉;
理由:根據left join特性不匹配顯示空,而where是針對關聯後的結果進行篩選
例如(where b.a=1)那也就說明'is null'不匹配的過濾掉..既然不匹配的過濾掉那完全可以改成join了;
情景② A left join B on 1=1 where .. 可以將left 去掉;
理由:left join 和 join 都能實現 on 1=1 笛卡爾乘積
情景③ A left join B on (a.a=X or/and b.a=X ) where b.任意欄位 =(只要不是is null), 可將left 去掉;
理由:(a.a=X or/and b.a=X ) 篩選後不匹配的顯示為空.. 所以where 傳值(只要不是is null)就可以將left去掉;
註釋:
很多時候即使寫left join A where a.欄位 is not null 情況。CBO生成執行計劃時會自己判斷成join的執行計劃;
此部落格僅是對left join的where 和on 2部分 做一些瞭解;
以上三種情況可以統稱:A left join B on ... where b.任意欄位 =(只要不是is null) 都可將left 去掉;
情景①
【on a.a = b.a 】部分
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b FROM dual) b 7 on a.a = b.a 8 where b.a =1; A B A B ---------- ---------- ---------- ---------- 1 2 1 2 SQL> |
等同於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b FROM dual) b 7 on a.a = b.a 8 where b.a =1; A B A B ---------- ---------- ---------- ---------- 1 2 1 2 SQL> |
|
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b FROM dual) b 7 on a.a = b.a 8 where b.a is null; A B A B ---------- ---------- ---------- ---------- 2 1 SQL> |
不等於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b FROM dual) b 7 on a.a = b.a 8 where b.a is null; A B A B ---------- ---------- ---------- ---------- SQL> |
|
|
|
情景②
【on 1 = 1 】部分
SQL> SELECT * 2 FROM (SELECT '1' a, 2 b 3 FROM dual 4 union all 5 SELECT '2' a, 1 b FROM dual) a 6 left join (SELECT '1' a, 2 b 7 FROM dual 8 union all 9 SELECT '' a, 3 b FROM dual) b 10 on (1 = 1) 11 where b.a =1; A B A B - ---------- - ---------- 1 2 1 2 2 1 1 2 SQL> |
等同於 |
SQL> SELECT * 2 FROM (SELECT '1' a, 2 b 3 FROM dual 4 union all 5 SELECT '2' a, 1 b FROM dual) a 6 /*left*/ join (SELECT '1' a, 2 b 7 FROM dual 8 union all 9 SELECT '' a, 3 b FROM dual) b 10 on (1 = 1) 11 where b.a =1; A B A B - ---------- - ---------- 1 2 1 2 2 1 1 2 SQL> |
情景③
【on (a.a = 1 or a.b =2)】部分
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (b.a = 2 or b.b = 5) 11 where b.b is null; A B A B ---------- ---------- ---------- ---------- 1 2 2 1 SQL> |
不等於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (b.a = 2 or b.b = 5) 11 where b.b is null; A B A B ---------- ---------- ---------- ---------- SQL> |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 2 or b.b = 5) 11 where b.b is not null; A B A B ---------- ---------- ---------- ---------- 2 1 1 2 2 1 1 3 SQL> |
等同於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 2 or b.b = 5) 11 where b.b is not null; A B A B ---------- ---------- ---------- ---------- 2 1 1 2 2 1 1 3 SQL> |
【on (a.a = 1 and a.b =2)】部分
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 1 and b.b = 3) 11 where b.b is null; A B A B ---------- ---------- ---------- ---------- 2 1 SQL> |
不等於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 1 and b.b = 3) 11 where b.b is null; A B A B ---------- ---------- ---------- ---------- SQL> |
|
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 left join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 1 and b.b = 3) 11 where b.b is not null; A B A B ---------- ---------- ---------- ---------- 1 2 1 3 SQL> |
等同於 |
SQL> SELECT * 2 FROM (SELECT 1 a, 2 b 3 FROM dual 4 union all 5 SELECT 2 a, 1 b FROM dual) a 6 /*left*/ join (SELECT 1 a, 2 b 7 FROM dual 8 union all 9 SELECT 1 a, 3 b FROM dual) b 10 on (a.a = 1 and b.b = 3) 11 where b.b is not null; A B A B ---------- ---------- ---------- ---------- 1 2 1 3 SQL> |
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28602568/viewspace-1280508/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL LEFT JOIN/ INNER JOIN/RIGHT JOINMySql
- mysql left join轉inner joinMySql
- join、inner join、left join、right join、outer join的區別
- sql中的join、left join、right joinSQL
- oracle update left join查詢Oracle
- Oracle Left join right jionOracle
- mysql + left joinMySql
- LEFT JOIN 和JOIN 多表連線
- sql:left join和join區別SQL
- Oracle資料庫聯接(inner join ,outer join)和NOT IN的特殊情況Oracle資料庫
- SQL Server Left joinSQLServer
- sql之left join、right join、inner join的區別SQL
- 連線查詢簡析 join 、 left join 、 right join
- sql left join 和 right join解釋SQL
- oracle的left join,right join和full join的一點介紹(R1)Oracle
- SQL中聯表查詢操作(LEFT JOIN, RIGHT JOIN, INNER JOIN)SQL
- MYSQL 怎麼 LEFT JOIN 多表聯查MySql
- 【MySQL】LEFT JOIN 踩坑MySql
- Inner Join, Left Outer Join和Association的區別
- sql的left join 、right join 、inner join之間的區別SQL
- mysql中的left join、right join 、inner join的詳細用法MySql
- left join,right join,inner join的條件on和where的區別
- sql server left join問題SQLServer
- MySql的join(連線)查詢 (三表 left join 寫法)MySql
- MySQL 之 LEFT JOIN 避坑指南MySql
- mysql left join 優化學習MySql優化
- sql的left join 命令詳解SQL
- Oracle中left join中右表的限制條件Oracle
- ORACLE MYSQL中join 欄位型別不同索引失效的情況OracleMySql型別索引
- mysql INNER JOIN、LEFT JOIN、RIGHT JOIN;內連線(等值連線)、左連線、右連線MySql
- LEFT JOIN 需要注意的點(Presto)REST
- mysql的left join和inner join的效率對比,以及如何優化MySql優化
- oracle知識整理(1) union和union all的區別,left join和right join的區別(各種join的區別)Oracle
- Oracle優化案例-用left join代替反連線 not in not exists(十)Oracle優化
- 深入Oracle的left join中on和where的區別詳解Oracle
- 【原】Oracle中Left Outer Join和外關聯(+)的區別Oracle
- MYSQL count標量子查詢改left joinMySql
- 用LEFT JOIN優化標量子查詢優化