欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【Leetcode】CombineTwoTables

程序员文章站 2022-05-14 10:05:01
  题目: table: person +————-+———+ | column name | type...

 

题目:
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

思路:
考察join语句
inner join是两表交集
outer join是两表除了交集之外的部分,left是不管左边表满不满足on的条件都在结果中,right同理,full是两表并集

算法:

select firstname,lastname,city,state from person p  left outer join address a on p.personid=a.personid