Sql简介 专题
专题目录
您的位置:database > Sql简介专题 > SQL Left Join子句
SQL Left Join子句
作者:--    发布时间:2019-11-20

在本教程中,我们将介绍另一种称为sql left join的连接,它用于从多个表中检索数据。

1. sql left join子句简介

在上一个教程中我们知道,如果两个表中至少有一行与连接条件匹配,则返回行记录的内联接。 内连接子句消除了与另一个表的行不匹配的行。

但是,左连接将返回左表中的所有行,而不管右表中是否存在匹配的行。

假设有两个表ab。表a有四行:1,2,34。表b还有四行:3,4,5,6

当将表a与表b连接时,表a中的所有行(左表)都包含在结果集中,而不管无论表b中是否存在匹配的行。

在sql中,使用以下语法将表a与表b连接起来。

select
    a.n
from
    a
left join b on b.n = a.n;

left join子句出现在from子句之后。 on关键字后面的条件称为连接条件b.n = a.n

2. sql left join示例

2.1. sql left join两个表的例子

我们来看看以下两个表:countrieslocations表的结构和关系。

每个地点属于一个且仅一个国家/地区,而每个国家/地区可以具有零个或多个地点。countrieslocations表之间的关系是一对多的。

locations表中的country_id列是链接到countries表中country_id列的外键。

要查询美国,英国和中国的国家/地区名称,请使用以下语句。

···

select
    country_id,
    country_name
from
    countries
where
    country_id in ('us', 'uk', 'cn');

执行上面查询语句,得到以下结果 -

+------------+--------------+
| country_id | country_name |
+------------+--------------+
| cn         | 中国         |
| uk         | 英国         |
| us         | 美国         |
+------------+--------------+
3 rows in set

以下查询检索位于美国,英国和中国的地点:

select
    country_id,
    street_address,
    city
from
    locations
where
    country_id in ('us', 'uk', 'cn');

执行上面查询语句,得到以下结果 -

+------------+------------------------------------------+---------------------+
| country_id | street_address                           | city                |
+------------+------------------------------------------+---------------------+
| us         | 2014 jabberwocky rd                      | southlake           |
| us         | 2011 interiors blvd                      | south san francisco |
| us         | 2004 charade rd                          | seattle             |
| uk         | 8204 arthur st                           | london              |
| uk         | magdalen centre, the oxford science park | oxford              |
+------------+------------------------------------------+---------------------+
5 rows in set

现在,使用left join子句将countries表与locations表连接为以下查询:

select
    c.country_name, c.country_id, l.country_id, l.street_address, l.city
from
    countries c
left join locations l on l.country_id = c.country_id
where
    c.country_id in ('us', 'uk', 'cn')

执行上面查询语句,得到以下结果 -

应用where子句中的条件,指示仅检索来自:usukchina行的数据。

因为使用left join子句,所以满足countries表的where子句中的条件的所有行都包含在结果集中。

对于countries表中的每一行,left join子句在locations表中查找匹配的行。如果找到至少一个匹配行,则数据库引擎将两个表中匹配行的列中的数据组合在一起。

如果没有找到匹配的行,例如,使用country_id的值是:cn,则countries表中的行包含在结果集中,而locations表中的行用null值填充。

由于右表中的非匹配行使用null值填充,因此可以将left join子句应用于表之间的未匹配行。

例如,要查找locations表中没有任何地点的国家/地区,请使用以下查询:

select
    country_name
from
    countries c
left join locations l on l.country_id = c.country_id
where
    l.location_id is null
order by
    country_name;

执行以下查询语句,得到以下结果 -

+--------------+
| country_name |
+--------------+
| 中国         |
| 丹麦         |
| 以色列       |
| 印度         |
| 埃及         |
| 墨西哥       |
| 尼日利亚     |
.....
| 荷兰         |
| 赞比亚       |
| 阿根廷       |
| 香港         |
+--------------+
21 rows in set

2.2. sql left join 3个表的示例

请参阅下表:regions, countrieslocations的结构和关系。

一个地区可能有零个或多个国家,而每个国家都位于一个地区。 countriesregions表之间的关系是一对多的。 countries表中的region_id列是国家和地区表之间的链接。

以下语句演示了如何连接3个表:regions, countrieslocations

通过上面的学习,现在您应该很好地理解sql left join子句的工作原理,并知道如何应用left join子句来查询来自多个表的数据。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册