在本教程中,我们将介绍另一种称为sql left join的连接,它用于从多个表中检索数据。
在上一个教程中我们知道,如果两个表中至少有一行与连接条件匹配,则返回行记录的内联接。 内连接子句消除了与另一个表的行不匹配的行。
但是,左连接将返回左表中的所有行,而不管右表中是否存在匹配的行。
假设有两个表a和b。表a有四行:1,2,3和4。表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。
我们来看看以下两个表:countries和locations表的结构和关系。
每个地点属于一个且仅一个国家/地区,而每个国家/地区可以具有零个或多个地点。countries和locations表之间的关系是一对多的。
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子句中的条件,指示仅检索来自:us,uk和china行的数据。
因为使用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
请参阅下表:regions, countries和locations的结构和关系。
一个地区可能有零个或多个国家,而每个国家都位于一个地区。 countries和regions表之间的关系是一对多的。 countries表中的region_id列是国家和地区表之间的链接。
以下语句演示了如何连接3个表:regions, countries和locations:
通过上面的学习,现在您应该很好地理解sql left join子句的工作原理,并知道如何应用left join子句来查询来自多个表的数据。