LeetCode 175. Combine Two Tables–Database–数据库题目
LeetCode题解专栏:LeetCode题解
我做的所有的LeetCode的题目都放在这个专栏里,大部分题目Java和Python的解法都有。
题目地址:Combine Two Tables - LeetCode
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.
Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
FirstName, LastName, City, State
题目意思也很简单,需要注意的是存在表1不存在表2的数据也要有,所以使用left outer join。
sql语句如下:
select FirstName, LastName, City, State
from Person
left outer join Address
on Person.PersonId = Address.PersonId;