一、MySQL JOIN 分类

  JOIN 按照功能大致分为如下三类:

    INNER JOIN(内连接):取得两个表中存在连接匹配关系的记录。

    LEFT JOIN(左连接):取得左表(table1)完全记录,即是右表(table2)并无对应匹配记录。

    RIGHT JOIN(右连接):与 LEFT JOIN 相反,取得右表(table2)完全记录,即是左表(table1)并无匹配对应记录。

二、图解关系

    INNER JOIN:用于取得两个表中存在连接匹配关系的记录。

MySQL JOIN 语法说明与 图解_JOIN

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值取代

MySQL JOIN 语法说明与 图解_JOIN_02



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表中没有的集合,在业务求新增的时候经常使用到的语法:
MySQL JOIN 语法说明与 图解_JOIN_03

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 |

+------+------+------+----+-------+-------+