Combine Two Tables Leetcode

able: 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

这道题目考察的是left join的应用,题目中提到不管有没有地址都从Person中选出来因此考虑用left join 或者right join,返回包含全部Person中的内容以及符合join条件的Address的内容。

如果用right join

# Write your MySQL query statement below
SELECT Person.FirstName,Person.LastName,Address.City,Address.State 
From Address
RIGHT join Person on Person.PersonId=Address.PersonId
如果用left join

# Write your MySQL query statement below
SELECT Person.FirstName,Person.LastName,Address.City,Address.State 
From Person
LEFT JOIN Address on Person.PersonId=Address.PersonId

二者是等价的

 




你可能感兴趣的:(LeetCode,JOIN,mysql)