Editing
Visual explanation of MySQL joins
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
使用文氏圖 (Venn diagram) 視覺化解釋 MySQL Joins (RIGHT Join、LEFT Join、INNER Join、OUTER Join) 的差異 == 摘要 == # 狀況 1-1: 以 a 資料為主,再把 b 資料黏上: LEFT JOIN #* 如果 a 資料有,但是 b 資料沒有。 b 欄位顯示 null # 狀況 2: 交集 (set intersection) 的資料,同時存在於 a 和 b: INNER JOIN #* 如果 a 資料有,但是 b 資料沒有。 不會顯示 # 狀況 3-1: 相對差集 (set difference) 的資料,存在於 a,但是不存在於 b: LEFT JOIN 並且加上 b 資料 id 欄位值是 null 做查詢 # 狀況 4: 聯集 (set union) 的資料,a 和 b 的所有資料 #* 如果 a 資料有,但是 b 資料沒有。 b 欄位顯示 null # 狀況 5: 對稱差集 (set symmetric difference) 的資料,只存在於 a 或 b 其中一個 == 準備測試資料 == 兩個資料表的 id 欄位都是主鍵( [http://www.w3schools.com/sql/sql_primarykey.asp PRIMARY KEY] )。 <pre> CREATE TABLE table_a (id int NOT NULL PRIMARY KEY, name varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL); INSERT INTO table_a (id, name) VALUES (1, '小一'), (2, '小二'), (3, '小三'), (4, '小四') ; CREATE TABLE table_b (id int NOT NULL PRIMARY KEY, gender varchar(30) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL); INSERT INTO table_b (id, gender) VALUES (1, '女'), (3, '女'), (5, '男'), (6, '不明') ; </pre> === 預覽測試資料 === 兩個資料表都有的資料,以紅色標示: <table> <tr> <td> table_a<br /> id name<br /> -- -------<br /> <span style="color:red;">1 小一</span><br /> 2 小二<br /> <span style="color:red;">3 小三</span><br /> 4 小四<br /> </td> <td> table_b<br /> id gender<br /> -- --------<br /> <span style="color:red;">1 女</span><br /> <span style="color:red;">3 女</span><br /> 5 男<br /> 6 不明<br /> </td> </table> == 狀況 1-1: 以 a 資料為主,再把 b 資料黏上 == {{exclaim}} 如果 a 資料比 b 資料的資料筆數少,將會以 a 資料筆數為主,部分 b 資料會遺失。 https://planetoid.info/images/Venn_diagrams_proposition_a.png MySQL 資料庫查詢: <pre> SELECT * FROM table_a LEFT JOIN table_b ON table_a.id = table_b.id; /*相同效果的別種寫法*/ SELECT * FROM table_a LEFT OUTER JOIN table_b ON table_a.id = table_b.id; </pre> === 預覽輸出資料 === <pre> 1|小一|1|女 3|小三|3|女 2|小二|NULL|NULL 4|小四|NULL|NULL </pre> == 狀況 1-2: 以 b 資料為主,再把 a 資料黏上 == https://planetoid.info/images/Venn_diagrams_proposition_b.png MySQL 資料庫查詢: <pre> SELECT * FROM table_a RIGHT JOIN table_b ON table_a.id = table_b.id; /*相同效果的別種寫法*/ SELECT * FROM table_a RIGHT OUTER JOIN table_b ON table_a.id = table_b.id; </pre> === 預覽輸出資料 === <pre> 1|小一|1|女 3|小三|3|女 NULL|NULL|5|男 NULL|NULL|6|不明 </pre> == 狀況 2: 交集的資料,同時存在於 a 和 b == https://planetoid.info/images/Venn_diagrams_intersection_of_two_sets.png MySQL 資料庫查詢: <pre> SELECT * FROM table_a INNER JOIN table_b ON table_a.id = table_b.id; /*相同效果的別種寫法*/ SELECT * FROM table_a, table_b WHERE table_a.id = table_b.id; SELECT a.*, b.* FROM table_a AS a NATURAL JOIN table_b AS b; -- INNER JOIN 寫成 JOIN SELECT * FROM table_a JOIN table_b ON table_a.id = table_b.id; </pre> === 預覽輸出資料 === <pre> 1|小一|1|女 3|小三|3|女 </pre> == 狀況 3-1: 相對差集的資料,存在於 a,但是不存在於 b == 存在於資料表 table_a ,但是不存在於 table_b https://planetoid.info/images/Venn_diagrams_relative_complement_of_b_(right)_in_a_(left).png MySQL 資料庫查詢: <pre> SELECT * FROM table_a LEFT JOIN table_b ON table_a.id = table_b.id WHERE table_b.id IS NULL; </pre> === 預覽輸出資料 === <pre> 2|小二|NULL|NULL 4|小四|NULL|NULL </pre> == 狀況 3-2: 相對差集的資料,存在於 b,但是不存在於 a == 存在於資料表 table_b ,但是不存在於 table_a https://planetoid.info/images/Venn_diagrams_relative_complement_of_a_(left)_in_b_(right).png MySQL 資料庫查詢: <pre> SELECT * FROM table_a RIGHT JOIN table_b ON table_a.id = table_b.id WHERE table_a.id IS NULL; </pre> === 預覽輸出資料 === <pre> NULL|NULL|5|男 NULL|NULL|6|不明 </pre> == 狀況 4: 聯集的資料,a 和 b 的所有資料 == https://planetoid.info/images/Venn_diagrams_union_of_two_sets.png MySQL 資料庫查詢 (相當於 SQL 的 FULL OUTER JOIN): <pre> SELECT * FROM table_a LEFT JOIN table_b ON table_a.id = table_b.id UNION SELECT * FROM table_a RIGHT JOIN table_b ON table_a.id = table_b.id; </pre> === 預覽輸出資料 === <pre> 1|小一|1|女 3|小三|3|女 2|小二|NULL|NULL 4|小四|NULL|NULL NULL|NULL|5|男 NULL|NULL|6|不明 </pre> == 狀況 5: 對稱差集的資料,只存在於 a 或 b 其中一個== 對稱差集 (Symmetric difference) 或稱 [https://zh.wikipedia.org/zh-tw/%E9%80%BB%E8%BE%91%E5%BC%82%E6%88%96 邏輯異或] (exclusive or) 的資料,只存在於 table_a 或 table_b 其中一個 https://planetoid.info/images/Venn_diagrams_symmetric_difference_of_two.png MySQL 資料庫查詢: <pre> SELECT * FROM table_a LEFT JOIN table_b ON table_a.id = table_b.id WHERE table_a.id IS NULL OR table_b.id IS NULL UNION SELECT * FROM table_a RIGHT JOIN table_b ON table_a.id = table_b.id WHERE table_a.id IS NULL OR table_b.id IS NULL; </pre> === 預覽輸出資料 === <pre> 2|小二|NULL|NULL 4|小四|NULL|NULL NULL|NULL|5|男 NULL|NULL|6|不明 </pre> == 參考資料 == * [http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/ A Visual Explanation of SQL Joins] * [http://stackoverflow.com/questions/38549/difference-between-inner-and-outer-joins sql - Difference between INNER and OUTER joins - Stack Overflow] * [http://blog.wu-boy.com/2009/01/mysqlleft-right-inner-outer-join-%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/ (MySQL)left, right, inner, outer join 使用方法 - 小惡魔 - 電腦技術 - 工作筆記 - AppleBOY] * [https://zh.wikipedia.org/wiki/Wikipedia:%E6%95%B0%E5%AD%A6%E4%B8%93%E9%A2%98/%E9%9B%86%E5%90%88%E8%AE%BA 維基百科:數學專題/集合論 - 維基百科,自由的百科全書] * Steve Stedman (2015). [http://stevestedman.com/2015/03/mysql-join-types-poster/ MySQL JOIN Types Poster - Steve Stedman] {{access | date = 2017-02-21}} * [https://zh.wikipedia.org/wiki/%E8%BF%9E%E6%8E%A5_(SQL) 連接 (SQL) - 維基百科,自由的百科全書] 圖片素材 * [https://en.wikipedia.org/wiki/Venn_diagram Venn diagram - Wikipedia, the free encyclopedia] [[Category:MySQL]] [[Category:Data Science]]
Summary:
Please note that all contributions to LemonWiki共筆 are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see
LemonWiki:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Access
(
view source
) (protected)
Template:Exclaim
(
edit
)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Current events
Recent changes
Random page
Help
Categories
Tools
What links here
Related changes
Special pages
Page information