mysql多表关联left join_Mysql多表表关联查询 inner Join left join right join

一.Join语法概述

join 用于多表中字段之间的联系,语法如下:... FROM table1 INNER|LEFT|RIGHT JOIN table2 ON conditiona

table1:左表;table2:右表。

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

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

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

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

注意:mysql不支持Full join,不过可以通过UNION 关键字来合并 LEFT JOIN 与 RIGHT JOIN来模拟FULL join.

接下来给出一个列子用于解释下面几种分类。如下两个表(A,B)mysql> select A.id,A.name,B.name from A,B where A.id=B.id;

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

| id | name | name |

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

| 1 | Pirate | Rutabaga |

| 2 | Monkey | Pirate |

| 3 | Ninja | Darth Vader |

| 4 | Spaghetti | Ninja |

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

4 rows in set (0.00 sec)

二.Inner join

内连接,也叫等值连接,inner join产生同时符合A和B的一组数据。mysql> select * from A inner join B on A.name = B.name;

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

| id | name | id | name |

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

| 1 | Pirate | 2 | Pirate |

| 3 | Ninja | 4 | Ninja |

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

mysql多表关联left join_Mysql多表表关联查询 inner Join left join right join_第1张图片

三.Left join

mysql> select * from A left join B on A.name = B.name;

#或者:select * from A left outer join B on A.name = B.name;

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

| id | name | id | name |

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

| 1 | Pirate | 2 | Pirate |

| 2 | Monkey | NULL | NULL |

| 3 | Ninja | 4 | Ninja |

| 4 | Spaghetti | NULL | NULL |

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

4 rows in set (0.00 sec)

left

你可能感兴趣的:(mysql多表关联left,join)