175. Combine Two Tables

前端之家收集整理的这篇文章主要介绍了175. Combine Two Tables前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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

按以上的数据格式查询,无论地址表有没有数据都要查询出来。

  • 因为Person表中有的数据可能在Address表中没有数据,所以需要使用right join,无论在Address中有没有数据都回查询出来
select 
 p.FirstName,p.LastName,a.City,a.State 
from 
 address a 
right join 
 Person p 
on a.PersonId = p.PersonId

猜你在找的程序笔记相关文章