LeetCode: 175. Combine Two Tables

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

解题思路

左外连接(Left Outer Join):以左表为主表的连接。接收左表的所有行,并用这些行与右表进行匹配。
右外连接(Right Outer Join):以右表为主表的连接。接收右表的所有行,并用这些行与右表进行匹配。
内连接(Inner Join):内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行。

本题是以 Person 表为主表的外链接。

AC 代码

select FirstName, LastName, City, State 
from Person 
left outer join Address 
on Person.PersonId = Address.PersonId;

猜你喜欢

转载自blog.csdn.net/yanglingwell/article/details/81623529