left join 是 left outer join 的简写,left join 默认是 outer 属性的。
account 表
custom 表
0 基础
inner join
left join
外连接包括 第一个表 的 所有行,但 仅仅 包含 第二个表 中那些匹配行 的数据。
1 左外连接 与 右外连接
关键词 left 指出连接左边的表决定结果集的行数,而右边的只负责提供与之匹配的列值。
2 三路外连接
SELECT a.account_id,
a.product_cd,
CONCAT(i.fname, ' ', i.lname) person_name,
b.name business_name
FROM account a
LEFT JOIN individual i
ON i.cust_id = a.cust_id
LEFT JOIN business b
ON a.cust_id = b.cust_id;
使用子查询
SELECT account_ind.account_id,
account_ind.product_cd,
account_ind.person_name,
b.name business_name
FROM
(SELECT a.account_id,
a.product_cd,
a.cust_id,
CONCAT(i.fname, ' ', i.lname) person_name
FROM account a
LEFT JOIN individual i
ON a.cust_id = i.cust_id) account_ind
LEFT JOIN business b
ON account_ind.cust_id = b.cust_id;