標籤:mysql join 文法說明與 圖解
一、MySQL JOIN 分類
JOIN 按照功能大致分為如下三類:
INNER JOIN(內串連):取得兩個表中存在串連匹配關係的記錄。
LEFT JOIN(左串連):取得左表(table1)完全記錄,即是右表(table2)並無對應匹配記錄。
RIGHT JOIN(右串連):與 LEFT JOIN 相反,取得右表(table2)完全記錄,即是左表(table1)並無匹配對應記錄。
二、圖解關係 INNER JOIN:用於取得兩個表中存在串連匹配關係的記錄。
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/89/C2/wKiom1gb3sjDU6M4AABL3sMjtiM021.png-wh_500x0-wm_3-wmp_4-s_4091312711.png" title="INNER.png" alt="wKiom1gb3sjDU6M4AABL3sMjtiM021.png-wh_50" />
mysql> select * from a inner join score on a.sn=score.sn;
+----+---------+-------+----+-------+-------+
| id | name | sn | id | sn | score |
+----+---------+-------+----+-------+-------+
| 1 | mashen | 10086 | 1 | 10086 | 90 |
| 2 | haishen | 10087 | 2 | 10087 | 59 |
| 3 | haoge | 10088 | 3 | 10088 | 77 |
+----+---------+-------+----+-------+-------+
3 rows in set (0.24 sec)
mysql> select * from a join score on a.sn=score.sn;
+----+---------+-------+----+-------+-------+
| id | name | sn | id | sn | score |
+----+---------+-------+----+-------+-------+
| 1 | mashen | 10086 | 1 | 10086 | 90 |
| 2 | haishen | 10087 | 2 | 10087 | 59 |
| 3 | haoge | 10088 | 3 | 10088 | 77 |
+----+---------+-------+----+-------+-------+
3 rows in set (0.00 sec)
Inner join
產生的結果集中,是A和B的交集。
當2個關聯表欄位相同時候 也可以用using(sn),這樣using 裡的欄位顯示一次.....
mysql> select * from a join score using(sn);
+-------+----+---------+----+-------+
| sn | id | name | id | score |
+-------+----+---------+----+-------+
| 10086 | 1 | mashen | 1 | 90 |
| 10087 | 2 | haishen | 2 | 59 |
| 10088 | 3 | haoge | 3 | 77 |
+-------+----+---------+----+-------+
LEFT JOIN:產生表A的完全集,而B表中匹配的則有值,沒有匹配的則以null值取代
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/89/BF/wKioL1gb4aTin9ugAABacAx2rfs302.png-wh_500x0-wm_3-wmp_4-s_320553044.png" title="LEFT.png" alt="wKioL1gb4aTin9ugAABacAx2rfs302.png-wh_50" />
mysql> select * from a left join score on a.sn=score.sn;
+----+-----------+-------+------+-------+-------+
| id | name | sn | id | sn | score |
+----+-----------+-------+------+-------+-------+
| 1 | mashen | 10086 | 1 | 10086 | 90 |
| 2 | haishen | 10087 | 2 | 10087 | 59 |
| 3 | haoge | 10088 | 3 | 10088 | 77 |
| 8 | left join | 11122 | NULL | NULL | NULL |
+----+-----------+-------+------+-------+-------+
4 rows in set (0.00 sec)
##產生在A表中有而在B表中沒有的集合,在業務求新增的時候經常使用到的文法:
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/89/C0/wKioL1gb4wizUb2UAABaj4Q8oqk331.png-wh_500x0-wm_3-wmp_4-s_2341308678.png" title="left1.png" alt="wKioL1gb4wizUb2UAABaj4Q8oqk331.png-wh_50" />
mysql> select * from a left join score on a.sn=score.sn where score.id is null;
+----+-----------+-------+------+------+-------+
| id | name | sn | id | sn | score |
+----+-----------+-------+------+------+-------+
| 8 | left join | 11122 | NULL | NULL | NULL |
+----+-----------+-------+------+------+-------+
1 row in set (0.01 sec)
##產生在b表中有而在a表中沒有的集合,就是INNER JOIN
mysql> select * from a left join score on a.sn=score.sn where score.id is not null
+----+---------+-------+------+-------+-------+
| id | name | sn | id | sn | score |
+----+---------+-------+------+-------+-------+
| 1 | mashen | 10086 | 1 | 10086 | 90 |
| 2 | haishen | 10087 | 2 | 10087 | 59 |
| 3 | haoge | 10088 | 3 | 10088 | 77 |
+----+---------+-------+------+-------+-------+
RIGHT JOIN:產生表B的完全集,而A表中匹配的則有值,沒有匹配的則以null值取代,與left join 相反.
mysql> select * from a right join score on a.sn=score.sn
-> ;
+------+---------+-------+----+-------+-------+
| id | name | sn | id | sn | score |
+------+---------+-------+----+-------+-------+
| 1 | mashen | 10086 | 1 | 10086 | 90 |
| 2 | haishen | 10087 | 2 | 10087 | 59 |
| 3 | haoge | 10088 | 3 | 10088 | 77 |
| NULL | NULL | NULL | 4 | 10089 | 77 |
| NULL | NULL | NULL | 5 | 10090 | 70 |
+------+---------+-------+----+-------+-------+
5 rows in set (0.00 sec)
##產生在b表中有而在a表中沒有的集合,沒有匹配顯示null
mysql> select * from a right join score on a.sn=score.sn where a.id is null;
+------+------+------+----+-------+-------+
| id | name | sn | id | sn | score |
+------+------+------+----+-------+-------+
| NULL | NULL | NULL | 4 | 10089 | 77 |
| NULL | NULL | NULL | 5 | 10090 | 70 |
+------+------+------+----+-------+-------+
本文出自 “DBSpace” 部落格,請務必保留此出處http://dbspace.blog.51cto.com/6873717/1869210
MySQL JOIN 文法說明與 圖解