理解full outer jion,union,union all
準備兩張測試表
09:41:52 kiwi@storedb1> select * from t1;
ID1 I
------------- -
1 A
2 B
3 C
Elapsed: 00:00:00.00
09:41:56 kiwi@storedb1> select * from t2;
I ID
- --
A A2
B B2
D D2
我們先來看看full outer join
09:42:14 kiwi@storedb1> select t1.id1,t1.id2,t2.id3 from t1 full outer join t2 on(t1.id2=t2.id2);
ID1 I ID
------------- - --
1 A A2
2 B B2
D2
3 C
再來看看union
09:44:54 kiwi@storedb1> select t1.id1,t1.id2,t2.id3 from t1 left outer join t2 on(t1.id2=t2.id2)
09:45:09 2 union
09:45:18 3 select t1.id1,t1.id2,t2.id3 from t1 right outer join t2 on(t1.id2=t2.id2);
ID1 I ID
------------- - --
1 A A2
2 B B2
3 C
D2
看看 union all
1 select t1.id1,t1.id2,t2.id3 from t1 left outer join t2 on(t1.id2=t2.id2)
2 union all
3* select t1.id1,t1.id2,t2.id3 from t1 right outer join t2 on(t1.id2=t2.id2)
09:46:20 kiwi@storedb1> /
ID1 I ID
------------- - --
1 A A2
2 B B2
3 C
1 A A2
2 B B2
D2
從中我們就可以看出區別full outer join 相當於分別做left outer join,right outer join 然後做一個union
union 是兩個的交集不包括重複的行,預設進行排序
而union all的效果是left outer join和right outer join的交集,包括了重複的集合,不進行排序
09:41:52 kiwi@storedb1> select * from t1;
ID1 I
------------- -
1 A
2 B
3 C
Elapsed: 00:00:00.00
09:41:56 kiwi@storedb1> select * from t2;
I ID
- --
A A2
B B2
D D2
我們先來看看full outer join
09:42:14 kiwi@storedb1> select t1.id1,t1.id2,t2.id3 from t1 full outer join t2 on(t1.id2=t2.id2);
ID1 I ID
------------- - --
1 A A2
2 B B2
D2
3 C
再來看看union
09:44:54 kiwi@storedb1> select t1.id1,t1.id2,t2.id3 from t1 left outer join t2 on(t1.id2=t2.id2)
09:45:09 2 union
09:45:18 3 select t1.id1,t1.id2,t2.id3 from t1 right outer join t2 on(t1.id2=t2.id2);
ID1 I ID
------------- - --
1 A A2
2 B B2
3 C
D2
看看 union all
1 select t1.id1,t1.id2,t2.id3 from t1 left outer join t2 on(t1.id2=t2.id2)
2 union all
3* select t1.id1,t1.id2,t2.id3 from t1 right outer join t2 on(t1.id2=t2.id2)
09:46:20 kiwi@storedb1> /
ID1 I ID
------------- - --
1 A A2
2 B B2
3 C
1 A A2
2 B B2
D2
從中我們就可以看出區別full outer join 相當於分別做left outer join,right outer join 然後做一個union
union 是兩個的交集不包括重複的行,預設進行排序
而union all的效果是left outer join和right outer join的交集,包括了重複的集合,不進行排序
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25462274/viewspace-2124997/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- `FULL JOIN` 和 `UNION ALL`
- union 和union all 使用區別
- union all和union的區別
- union和union all 關鍵字
- msyql jion 和 union 的區別
- MySQL學習(五) UNION與UNION ALLMySql
- sql中union和union all的用法SQL
- sql中UNION和UNION ALL的區別SQL
- Oracle的集合操作(union、union all、intersect、minus集合函式)Oracle函式
- union all 最佳化案例
- MYSQL學習筆記24: 多表查詢(聯合查詢,Union, Union All)MySql筆記
- union用法
- union注入
- OceanBase 金融專案最佳化案例(union all 改寫)
- 銀彈谷V平臺VSQL使用distinct與union all使用SQL
- C語言:一個例子理解 union 和 structC語言Struct
- oracle知識整理(1) union和union all的區別,left join和right join的區別(各種join的區別)Oracle
- union存取低高位
- union和enum使用
- union 聯合體
- MySQL中使用or、in與union all在查詢命令下的效率對比MySql
- MySQL, Incorrect usage of UNION and ORDER BYMySql
- 組合查詢(UNION)
- [CF1517F] Union
- 並查集(Union Find)並查集
- Union Find程式碼塊
- sql注入之union注入SQL
- Analysis of Set Union Algorithms 題解Go
- Oracle優化案例-union代替or(九)Oracle優化
- SQL優化案例-union代替or(九)SQL優化
- Struct 和 Union有下列區別Struct
- SQLite語句(三):JOIN和UNIONSQLite
- WPF Path GeometryCombineMode Union, Exclude,Intersect,Xor
- 備忘:union()後paginate分頁
- SQL最佳化案例-union代替or(九)SQL
- 4. union-find演算法演算法
- mysql求交集:UNION ALL合併查詢,inner join內連線查詢,IN/EXISTS子查詢MySql
- Oracle優化案例-又見union代替or(二十)Oracle優化
- PostgreSQL 原始碼解讀(30)- 查詢語句#15(查詢優化-扁平化處理UNION ALL)SQL原始碼優化