175.Combine Two Tables
175.Combine Two Tables
难度:Easy
SQL架构
1
表1: Person
2
3
+-------------+---------+
4
| 列名 | 类型 |
5
+-------------+---------+
6
| PersonId | int |
7
| FirstName | varchar |
8
| LastName | varchar |
9
+-------------+---------+
10
PersonId 是上表主键
11
表2: Address
12
13
+-------------+---------+
14
| 列名 | 类型 |
15
+-------------+---------+
16
| AddressId | int |
17
| PersonId | int |
18
| City | varchar |
19
| State | varchar |
20
+-------------+---------+
21
AddressId 是上表主键
Copied!
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
FirstName, LastName, City, State
代码如下:
1
# Write your MySQL query statement below
2
SELECT p.FirstName,p.LastName,a.City,a.State FROM Address a RIGHT JOIN Person p on p.PersonId=a.PersonId;
Copied!
Copy link