Visual explanation of MySQL joins: Difference between revisions

Jump to navigation Jump to search
no edit summary
No edit summary
Line 48: Line 48:
</table>
</table>


== 狀況1: 交集的資料,同時存在於 a 和 b ==
== 狀況 1-1: 以 a 資料為主,再把 b 資料黏上 ==
http://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;
</pre>
 
=== 預覽輸出資料 ===
<pre>
1|小一|1|女
3|小三|3|女
2|小二|NULL|NULL
4|小四|NULL|NULL
</pre>
 
== 狀況 1-2: 以 b 資料為主,再把 a 資料黏上 ==
http://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;
</pre>
 
=== 預覽輸出資料 ===
<pre>
1|小一|1|女
3|小三|3|女
NULL|NULL|5|男
NULL|NULL|6|不明
</pre>
 
== 狀況 2: 交集的資料,同時存在於 a 和 b ==
http://planetoid.info/images/Venn_diagrams_intersection_of_two_sets.png
http://planetoid.info/images/Venn_diagrams_intersection_of_two_sets.png


Line 69: Line 105:
</pre>
</pre>


== 狀況2-1: 相對差集的資料,存在於 a,但是不存在於 b ==
== 狀況 3-1: 相對差集的資料,存在於 a,但是不存在於 b ==
存在於資料表 table_a ,但是不存在於 table_b
存在於資料表 table_a ,但是不存在於 table_b


Line 87: Line 123:
4|小四|NULL|NULL
4|小四|NULL|NULL
</pre>
</pre>
== 狀況2-2: 相對差集的資料,存在於 b,但是不存在於 a ==
== 狀況 3-2: 相對差集的資料,存在於 b,但是不存在於 a ==
存在於資料表 table_b ,但是不存在於 table_a
存在於資料表 table_b ,但是不存在於 table_a


Line 106: Line 142:
</pre>
</pre>


== 狀況3: 聯集的資料,a 和 b 的所有資料 ==
== 狀況 4: 聯集的資料,a 和 b 的所有資料 ==
http://planetoid.info/images/Venn_diagrams_union_of_two_sets.png
http://planetoid.info/images/Venn_diagrams_union_of_two_sets.png


Line 132: Line 168:
</pre>
</pre>


== 狀況4: 對稱差的資料,只存在於 a 或 b 其中一個==
== 狀況 5: 對稱差的資料,只存在於 a 或 b 其中一個==
對稱差 (Symmetric difference)的資料,只存在於 table_a 或 table_b 其中一個
對稱差 (Symmetric difference)的資料,只存在於 table_a 或 table_b 其中一個


Navigation menu