MySQL 关键字 join
有时候我们为了得到完整的结果,我们需要从两个或者多个表中获取我们的结果,需要用到 join 关键字。
- inner join
- left join
- right join
- full join
图例:
假如咱们有两张表
- customers id name …
- orders id customer_id amount …
-
列出所有顾客的订单
select * from customers inner join orders on customers.id = orders.customer_id
-
列出所有顾客以及他们的订单(如果有的话)
select * from customers left join orders on customers.id = orders.customer_id
-
列出所有订单,以及购买它们的顾客(如果有的话)
select * from customers right join orders on customers.id = orders.customer_id
-
列出所有顾客,所有订单
select * from customers full join orders on customers.id = orders.customer_id