MySQL INNER JOIN 关键字(或有时称为简单 JOIN)用于组合两个表的列值,并在列之间存在匹配时返回两个表中的所有行。
语法
在 MySQL 中使用 INNER JOIN 关键字的语法如下:
SELECT table1.column1, table1.column2, table2.column1, table2.column2, ...
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
示例:
考虑名为Employee和Contact_Info的数据库表,其中包含以下记录:
表 1:Employee 表
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
表 2:Contact_Info 表
Phone_Number | EmpID | Address | Gender |
---|---|---|---|
+1-8054098000 | 2 | Brooklyn, New York, USA | F |
+33-147996101 | 3 | Grenelle, Paris, France | M |
+31-201150319 | 4 | Geuzenveld, Amsterdam, Netherlands | F |
+86-1099732458 | 6 | Yizhuangzhen, Beijing, China | M |
+65-67234824 | 7 | Yishun, Singapore | M |
+81-357799072 | 8 | Koto City, Tokyo, Japan | M |
要内连接 Employee和Contact_Info表基于匹配列EmpID,查询如下。这将从 Employee 表中获取 Name 和 Age 列,并从 Contact_Info 中获取 Address 列
SELECT Employee.Name, Employee.Age, Contact_Info.Address
FROM Employee
INNER JOIN Contact_Info
ON Employee.EmpID = Contact_Info.EmpID;
这将产生如下所示的结果:
Name | Age | Address |
---|---|---|
Marry | 24 | Brooklyn, New York, USA |
Jo | 27 | Grenelle, Paris, France |
Kim | 30 | Geuzenveld, Amsterdam, Netherlands |
Huang | 28 | Yizhuangzhen, Beijing, China |
要获取表的所有字段,使用table.*关键字,例如 - 要获取Employee表的所有字段,Employee.*在下面的MySQL代码中使用:
SELECT Employee.*, Contact_Info.Address
FROM Employee
INNER JOIN Contact_Info
ON Employee.EmpID = Contact_Info.EmpID;
以下代码的结果将是:
EmpID | Name | City | Age | Salary | Address |
---|---|---|---|---|---|
2 | Marry | New York | 24 | 2750 | Brooklyn, New York, USA |
3 | Jo | Paris | 27 | 2800 | Grenelle, Paris, France |
4 | Kim | Amsterdam | 30 | 3100 | Geuzenveld, Amsterdam, Netherlands |
6 | Huang | Beijing | 28 | 2800 | Yizhuangzhen, Beijing, China |
连接三个表
要连接三个表,只需简单地反复重复 JOIN 子句。请参阅下面的语法,其中 INNER JOIN 子句用于连接三个表。任何其他 JOIN 类型都可以以相同的方式使用。同样,它可以用于连接任意数量的表。
语法
MySQL 中使用 INNER JOIN 关键字连接三个表的语法如下:
SELECT table1.column1, table1.column2, ...
table2.column1, table2.column2, ...
table3.column1, table3.column2, ...
FROM table1 INNER JOIN table2
ON table1.matching_column = table2.matching_column
INNER JOIN table3
ON table1.matching_column = table3.matching_column;
示例:
考虑一个包含名为 Employee、Bonus_Paid 和 Contact_Info 的表的数据库,其内容如下记录:
表1:Employee表
EmpID | Name | City | Age | Salary |
---|---|---|---|---|
1 | John | London | 25 | 3000 |
2 | Marry | New York | 24 | 2750 |
3 | Jo | Paris | 27 | 2800 |
4 | Kim | Amsterdam | 30 | 3100 |
5 | Ramesh | New Delhi | 28 | 3000 |
6 | Huang | Beijing | 28 | 2800 |
表2: Bonus_Paid 表
EmpID | Bonus |
---|---|
1 | 500 |
2 | 400 |
3 | 450 |
4 | 550 |
5 | 400 |
6 | 600 |
表 3:Contact_Info 表
Phone_Number | EmpID | Address | Gender |
---|---|---|---|
+1-8054098000 | 2 | Brooklyn, New York, USA | F |
+33-147996101 | 3 | Grenelle, Paris, France | M |
+31-201150319 | 4 | Geuzenveld, Amsterdam, Netherlands | F |
+86-1099732458 | 6 | Yizhuangzhen, Beijing, China | M |
+65-67234824 | 7 | Yishun, Singapore | M |
+81-357799072 | 8 | Koto City, Tokyo, Japan | M |
从这些表中获取包含EmpID、Name、Bonus和Address的记录,可以使用以下查询:
SELECT Employee.EmpID, Employee.Name,
Bonus_Paid.Bonus, Contact_Info.Address
FROM Employee INNER JOIN Bonus_Paid
ON Employee.EmpID = Bonus_Paid.EmpID
INNER JOIN Contact_Info
ON Employee.EmpID = Contact_Info.EmpID;
这将产生如下所示的结果:
EmpID | Name | Bonus | Address |
---|---|---|---|
2 | Marry | 400 | Brooklyn, New York, USA |
3 | Jo | 450 | Grenelle, Paris, France |
4 | Kim | 550 | Geuzenveld, Amsterdam, Netherlands |
6 | Huang | 600 | Yizhuangzhen, Beijing, China |
要获取上述记录,但条件如 Bonus 应大于等于 500,可以使用以下查询:
SELECT Employee.EmpID, Employee.Name,
Bonus_Paid.Bonus, Contact_Info.Gender,
Contact_Info.Address
FROM Employee INNER JOIN Bonus_Paid
ON Employee.EmpID = Bonus_Paid.EmpID
INNER JOIN Contact_Info
ON Employee.EmpID = Contact_Info.EmpID
WHERE Bonus_Paid.Bonus >= 500;
以下结果代码为:
EmpID | Name | Bonus | Address |
---|---|---|---|
4 | Kim | 550 | Geuzenveld, Amsterdam, Netherlands |
6 | Huang | 600 | Yizhuangzhen, Beijing, China |