1、题目
题目链接:https://leetcode.cn/problems/combine-two-tables/
SQL架构
Create table If Not Exists Person (personId int, firstName varchar(255), lastName varchar(255))
Create table If Not Exists Address (addressId int, personId int, city varchar(255), state varchar(255))
Truncate table Person
insert into Person (personId, lastName, firstName) values ('1', 'Wang', 'Allen')
insert into Person (personId, lastName, firstName) values ('2', 'Alice', 'Bob')
Truncate table Address
insert into Address (addressId, personId, city, state) values ('1', '2', 'New York City', 'New York')
insert into Address (addressId, personId, city, state) values ('2', '3', 'Leetcode', 'California')
表: Person
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
personId 是该表的主键列。
该表包含一些人的 ID 和他们的姓和名的信息。
表: Address
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
addressId 是该表的主键列。
该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
编写一个SQL查询来报告 Person 表中每个人的姓、名、城市和州。如果 personId 的地址不在 Address 表中,则报告为空 null 。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 1:
输入:
Person表:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address表:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
输出:
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
解释:
地址表中没有 personId = 1 的地址,所以它们的城市和州返回 null。
addressId = 1 包含了 personId = 2 的地址信息。
2、解法
解法1:
一开始因为前面的题很少用连接了,所以这题一开始想着用判断来做。这种也是可以做出来的,不过太多查询了。对新加入的列进行判断获取对应数据。
select p.firstName firstName ,
p.lastName lastName ,
if(p.personId in (select personId from Address), (select city from Address a where p.personId = a.personId ) , null) city ,
case when (p.personId in (select personId from Address)) then (select state from Address a where p.personId = a.personId )
else null end state
from Person p
解法2:
这里利用了left join on,左连接:即使右表中没有匹配,也从左表返回所有的行,这样就行了
select p.firstName firstName , p.lastName lastName , a.city city , a.state state from Person p left join Address a on p.personId = a.personId
知识补充
各种连接
如果你想看看相关案例可以看https://www.runoob.com/sql/sql-join-full.html
- INNER JOIN:如果表中有至少一个匹配,则返回行
- LEFT JOIN:即使右表中没有匹配,也从左表返回所有的行
- RIGHT JOIN:即使左表中没有匹配,也从右表返回所有的行
- FULL JOIN:只要其中一个表中存在匹配,则返回行
inner join 这个如果没有匹配数据行,那么该数据行就不会出现在新表中。
交集

left join从左边的表开始加入数据,左边数据全部保留,根据匹配数据是否存在往右边添加数据,如果没有匹配数据则为null。

right join 保留右边的表全部数据,左边的表根据匹配条件加入数据,如果没有匹配数据则为null。

full join全部数据保留,如果对方没有匹配数据则对方数据变为null
并集


这篇博客通过LeetCode的题目介绍如何使用SQL中的左连接(left join)来合并两个表。作者首先给出了题目链接和查询要求,然后分别提供了两种解法,重点讲解了利用left join的方法。最后,博客补充了关于各种类型的SQL连接(如INNER JOIN、LEFT JOIN、RIGHT JOIN和FULL JOIN)的知识。
1680

被折叠的 条评论
为什么被折叠?



