Buckle 175. Combine two watches
https://leetcode-cn.com/problems/combine-two-tables/
Table 1: Person
+ ------------- + --------- +
| Column name | Type |
+ ------------- + ---- ----- +
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+ ------------- + --------- +
PersonId is the table Primary key
Table 2: Address
+ ------------- + --------- +
| Column name | Type |
+ ------------- + ---- ----- +
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+ ------------- + --------- +
AddressId is the primary key of the above table
Write an SQL query that satisfies the conditions: Whether or not person has address information, the following information of person needs to be provided based on the above two tables:
FirstName, LastName, City, State
Combine, connect
Generally left connection (because the left connection is easy to transplant) A left join B on equivalent condition (ID equal or something)
# Write your MySQL query statement below
#组合连接
select FirstName, LastName, City, State
from Person left join Address on Person.PersonId=Address.PersonId;
See "MySQL Interview Questions & Study Notes"
https://mp.csdn.net/console/editor/html/105337616
Left join (A left join B on cat = id)
For porting compatibility, try to use the left connection
Inner connection: the intersection of left and right connections (A inner join B on cat = id)
Outer connection: union of left and right connections (mysql does not support) (A outer join B on cat = id)
Title: Interpretation of Interview Questions on the Left
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;