力扣175. 组合两个表
https://leetcode-cn.com/problems/combine-two-tables/
表1: Person
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId 是上表主键
表2: Address
+-------------+---------+
| 列名 | 类型 |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId 是上表主键
编写一个 SQL 查询,满足条件:无论 person 是否有地址信息,都需要基于上述两表提供 person 的以下信息:
FirstName, LastName, City, State
组合,连接
一般都是左连接(因为左连接方便移植)A left join B on 等价条件(ID相等什么的)
# Write your MySQL query statement below
#组合连接
select FirstName, LastName, City, State
from Person left join Address on Person.PersonId=Address.PersonId;
见《MySQL面试题&学习笔记》
https://mp.csdn.net/console/editor/html/105337616
左连接(A left join B on cat=id)
出于移植兼容性,尽量采用左连接
内连接:是左右连接的交集(A inner join B on cat=id)
外连接:是左右连接的并集(mysql不支持)(A outer join B on cat=id)
题目:左连接面试题讲解
select mid,hid,h.tname,mres,gid,g.tname
from ((m left join n as h on m.hid=h.tid)left join n as g on m.gid=g.tid)
order by mid;