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;
全部评论

相关推荐

阿武同学:基本信息保留前面三行,其他的可以全部删掉,邮箱最重要的你没写,主修课程精简到8个以内,实习里面2/3/4都是水内容的,非要写的话建议两到三句话,项目经历排版优化下,自我评价缩到三行
点赞 评论 收藏
分享
年纪大的小汤姆:哥们你是不是真和这人有仇😨
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务