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

相关推荐

不愿透露姓名的神秘牛友
06-26 14:50
人力小鱼姐:有后面墨迹那两句的时间问题早回答完了
点赞 评论 收藏
分享
06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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