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二者是等价的
本文介绍了一个SQL查询案例,该案例使用左连接从Person表中选取所有人员的信息,并尽可能地匹配Address表中的地址详情,即使某些人员没有对应的地址记录。
745

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



