连接

在实际运用中,我们常常会将多张表连接在一起进行查询,在一张表中使用外键跟另一张表进行关联。

比如有如下两张表:

mysql> SELECT * FROM Websites;
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
| 1  | Google       | //www.google.cm/    | 1     | USA     |
| 2  | 淘宝          | //www.taobao.com/   | 13    | CN      |
| 3  | 菜鸟教程      | http://www.runoob.com/    | 4689  | CN      |
| 4  | 微博          | http://weibo.com/         | 20    | CN      |
| 5  | Facebook     | //www.facebook.com/ | 3     | USA     |
| 7  | stackoverflow | http://stackoverflow.com/ |   0 | IND     |
+----+---------------+---------------------------+-------+---------+
mysql> SELECT * FROM access_log;
+-----+---------+-------+------------+---------+
| aid | site_id | count | date       | country |
+-----+---------+-------+------------+---------+
|   1 |       1 |    45 | 2016-05-10 | USA     |
|   2 |       3 |   100 | 2016-05-13 | JP     |
|   3 |       1 |   230 | 2016-05-14 | CN     |
|   4 |       2 |    10 | 2016-05-14 | USA     |
|   5 |       5 |   205 | 2016-05-14 | CN     |
|   6 |       4 |    13 | 2016-05-15 | USA     |
|   7 |       3 |   220 | 2016-05-15 | USA     |
|   8 |       5 |   545 | 2016-05-16 | USA     |
|   9 |       3 |   201 | 2016-05-17 | IND     |
|  10 |       6 |   100 | 2016-05-18 | ENG     |
+-----+---------+-------+------------+---------+

"Websites" 表中的 "id" 列指向 "access_log" 表中的字段 "site_id"。上面这两个表是通过 "site_id" 列联系起来的。

Join 类型

  • INNER JOIN:如果表中有至少一个匹配,则返回行
  • LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
  • RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
  • FULL JOIN:只要其中一个表中存在匹配,则返回行

内连接

INNER JOIN 关键字在表中存在至少一个匹配时返回行,可以看做是集合中的交集。

注释

INNER JOIN 与 JOIN 是相同的。

基础语法:

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
-- or
SELECT column_name(s)
FROM table1
JOIN table2
ON table1.column_name=table2.column_name;

比如:

SELECT Websites.name, access_log.count, access_log.date
FROM Websites
INNER JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count;

查询出的数据如下:

+--------------+--------+--------------+
| name         |  count |   date       |
+--------------+--------+--------------+
| 淘宝          |   10   |  2016-05-14  |
| 微博          |   13   |  2016-05-15  |
| Google       |   45   |  2016-05-10  |
| 菜鸟教程       |  100    |  2016-05-13  |
| 菜鸟教程       |  201    |  2016-05-17  |
| Facebook      |  205    |  2016-05-14  |
| 菜鸟教程       |  220    |  2016-05-15  |
| Google       |   230   |  2016-05-14  |
| stackoverflow |  545    |  2016-05-16  |
+---------------+--------+--------------+

左连接

LEFT JOIN 关键字从左表(table1)返回所有的行,即使右表(table2)中没有匹配。如果右表中没有匹配,则结果为 NULL。

注释

在某些数据库中,LEFT JOIN 称为 LEFT OUTER JOIN。

基础语法:

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
-- or
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;

比如:

SELECT Websites.name, access_log.count, access_log.date
FROM Websites
LEFT JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count;

查询出的数据如下:

+--------------+--------+--------------+
| name         |  count |   date       |
+--------------+--------+--------------+
| 淘宝          |   10   |  2016-05-14  |
| 微博          |   13   |  2016-05-15  |
| Google       |   45   |  2016-05-10  |
| 菜鸟教程       |  100    |  2016-05-13  |
| 菜鸟教程       |  201    |  2016-05-17  |
| Facebook      |  205    |  2016-05-14  |
| 菜鸟教程       |  220    |  2016-05-15  |
| Google       |   230   |  2016-05-14  |
| Facebook     |  545    |  2016-05-16  |
| stackoverflow |  NULL    |  NULL  |
+---------------+--------+--------------+

右连接

RIGHT JOIN 关键字从右表(table2)返回所有的行,即使左表(table1)中没有匹配。如果左表中没有匹配,则结果为 NULL。

注释

在某些数据库中,RIGHT JOIN 称为 RIGHT OUTER JOIN。

基础语法:

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
-- or
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;

比如:

SELECT Websites.name, access_log.count, access_log.date
FROM Websites
RIGHT JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count;

查询出的数据如下:

+--------------+--------+--------------+
| name         |  count |   date       |
+--------------+--------+--------------+
| 淘宝          |   10   |  2016-05-14  |
| 微博          |   13   |  2016-05-15  |
| Google       |   45   |  2016-05-10  |
| 菜鸟教程       |  100    |  2016-05-13  |
| NULL         |   100   |  2016-05-18 |
| 菜鸟教程       |  201    |  2016-05-17  |
| Facebook      |  205    |  2016-05-14  |
| 菜鸟教程       |  220    |  2016-05-15  |
| Google       |   230   |  2016-05-14  |
| Facebook     |  545    |  2016-05-16  |
+---------------+--------+--------------+

全连接

FULL OUTER JOIN 关键字只要左表(table1)和右表(table2)其中一个表中存在匹配,则返回行.

FULL OUTER JOIN 关键字结合了 LEFT JOIN 和 RIGHT JOIN 的结果,相当于集合中的并集。

注释

在某些数据库中,RIGHT JOIN 称为 RIGHT OUTER JOIN。

基础语法:

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;

比如:

SELECT Websites.name, access_log.count, access_log.date
FROM Websites
FULL OUTER JOIN access_log
ON Websites.id=access_log.site_id
ORDER BY access_log.count DESC;

查询出的数据如下:

+--------------+--------+--------------+
| name         |  count |   date       |
+--------------+--------+--------------+
| 淘宝          |   10   |  2016-05-14  |
| 微博          |   13   |  2016-05-15  |
| Google       |   45   |  2016-05-10  |
| 菜鸟教程       |  100    |  2016-05-13  |
| NULL         |   100   |  2016-05-18 |
| 菜鸟教程       |  201    |  2016-05-17  |
| Facebook      |  205    |  2016-05-14  |
| 菜鸟教程       |  220    |  2016-05-15  |
| Google       |   230   |  2016-05-14  |
| Facebook     |  545    |  2016-05-16  |
| stackoverflow |  NULL    |  NULL     |
+---------------+--------+--------------+

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress