Leetcode-SQL
链接:https://leetcode-cn.com/problems/combine-two-tables/solution/zu-he-liang-ge-biao-by-leetcode/
习题来源:力扣(LeetCode)
175-组合两个表-2021-01-09
点击查看
表1:Person
列名 | 类型 |
---|---|
PersonID | int |
FirstName | varchar |
LastName | varchar |
*** ----------------------
PresonID 是上表主键
表2:Address
列名 | 类型 |
---|---|
AddressId | int |
PersonId | int |
City | varchar |
State | varchar |
*** ----------------------
AddressId 是上表主键
要求:编写一个SQL查询,满足条件;无论person是否有地址信息,都需要基于上述两表提供person的以下信息:
FirstName | LastName | City | State |
---|
点击查看
--我的答案
select
FirstName,
LastName,
City,
state
from
(
select
PersonId,
FirstName,
LastName
from
Person
) a
left join (
select
PersonId,
City,
State
from
Address
) b on a.PersonId = b.PersonId
--官方建议答案
select FirstName, LastName, City, State
from Person left join Address
on Person.PersonId = Address.PersonId