MySQL JOIN 文法說明與 圖解

來源:互聯網
上載者:User

標籤: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 文法說明與 圖解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.