關於外連線和where條件
在外連線中,where後出現的表等同於內連線,因此,如果用了where條件,就應當將left join改為inner join。以下測試驗證了這點。
with tab_a as
(
select 1 id1, 11 id2 from dual union all
select 2 id1, 22 id2 from dual union all
select 3 id1, 33 id2 from dual
), tab_b as
(
select 1 id1, 11 id2 from dual union all
select 2 id1, 22 id2 from dual union all
select 4 id1, 44 id2 from dual
)
select a.*, b.*
from tab_a a
left join tab_b b on a.id1 = b.id1
where b.id1 <> 4;
結果是沒有3的
ID1 ID2 ID1 ID2
---------- ---------- ---------- ----------
1 11 1 11
2 22 2 22
如果不加where條件
with tab_a as
(
select 1 id1, 11 id2 from dual union all
select 2 id1, 22 id2 from dual union all
select 3 id1, 33 id2 from dual
), tab_b as
(
select 1 id1, 11 id2 from dual union all
select 2 id1, 22 id2 from dual union all
select 4 id1, 44 id2 from dual
)
select a.*, b.*
from tab_a a
left join tab_b b on a.id1 = b.id1
--where b.id1 <> 4;
結果是包含3的
ID1 ID2 ID1 ID2
---------- ---------- ---------- ----------
1 11 1 11
2 22 2 22
3 33
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28974745/viewspace-2216092/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 資料庫外連線,自然連線,內連線,條件連線,等值連線關係及詳解資料庫
- 外連線有 OR 關聯條件只能走 NL優化優化
- 關於在SQL語句中ON和WHERE中條件使用的差異SQL
- switch拼接where條件
- 替代SQL語句WHERE條件中OR關鍵詞SQL
- SQL中on條件與where條件的區別[轉]SQL
- 關於條件變數變數
- MySQL核心技術之“WHERE條件”MySql
- sql 內連線和外連線SQL
- 儲存過程WHERE條件不生效儲存過程
- LINQ系列:LINQ to SQL Where條件SQL
- 連線條件是兩個欄位“or”關係的SQL優化SQL優化
- Oracle 在連線條件裡處理和比較 NULL 值OracleNull
- 連線條件字元型別不一樣。字元型別
- 解密SVM系列(一):關於拉格朗日乘子法和KKT條件解密
- left join,right join,inner join的條件on和where的區別
- Oracle 連線條件中帶有OR的測試Oracle
- Oracle內連線、外連線、右外連線、全外連線小總結Oracle
- mysql,where條件查詢等學習筆記MySql筆記
- MongoDB之資料查詢(where條件過濾)MongoDB
- mysql 左連結 left join 條件寫在where 後面與 on後面的區別MySql
- 關於面向連線與面向無連線
- 【SQL】一條外連線和內連線混合使用的SQL語句搞定同事一迷茫需求SQL
- 【SQL】Oracle的內連線、左外連線、右外連線及全外連線SQLOracle
- Oracle左外連線、右外連線、完全外連線以及(+)號用法Oracle
- Oracle 左外連線、右外連線、全外連線小總結Oracle
- 內連線、外連線
- ORACLE sql merge into update where條件位置與效能消耗OracleSQL
- Percona MySQL 5.6 WHERE 條件中 OR 的索引測試MySql索引
- connect by與where條件執行順序問題
- mysql where條件中 字串右邊的空格會忽略MySql字串
- 當long欄位在where條件裡的解決方案
- MYSQL學習筆記6: DQL條件查詢(where)MySql筆記
- SQL的四種連線:內連線 左外連線 右外連線 全連線SQL
- SQL Server中內連線和外連線的區別SQLServer
- 關於jetty連線關閉問題Jetty
- 【Oracle】where條件執行順序(上篇的問題延伸)Oracle
- 資料泵過濾匯出資料的where條件