简介
本文介绍mysql join的几种写法,假设有如下两张表:
customer:
id | customer_id |
---|---|
1 | 11 |
2 | 12 |
order:
id | customer_id |
---|---|
1 | 10 |
2 | 11 |
join
-
(inner) join:返回两张表中满足连接关系的记录,不存在NULL值
select * from customer join order on customer.customer_id = order.customer_id;
-
left (outer) join:返回左表的全部记录和满足连接关系的记录,右表可能有NULL值
select * from customer left join order on customer.customer_id = order.customer_id;
-
right (outer) join:返回右表的全部记录和满足连接关系的记录,左表可能有NULL值
select * from customer right join order on customer.customer_id = order.customer_id;
总结
这三种连接关系可以用下图表示: