MySQL LEFT JOIN 3表,怎么做

时间:2015-08-25 18:49:15

标签: mysql

我有3张桌子

warehouse(id,name)
article (id, name)
inventory(id,warehouse_id,article_id, quantity)

在文章中我有1000篇文章,我有4个仓库。现在在我的表库存中,我只有300个关节的记录,只有2个仓库。我想用他们所有的仓库获得所有文章,但我只能获得300行。我正在尝试离开加入,但我无法得到它。

For example i want this
article 1: 
warehouse 1:1000
warehouse 2:1000
warehouse 3:0  (beacuse there is no record in invetory)
warehouse 4:3000

article 2:  
warehouse 1:0 (beacuse there is no record in invetory)
warehouse 2:0 (beacuse there is no record in invetory)
warehouse 3:0 (beacuse there is no record in invetory)
warehouse 4:0 (beacuse there is no record in invetory)

谢谢,对不起,如果问题已经存在,但我尝试了这些答案,我无法解决我的问题

1 个答案:

答案 0 :(得分:0)

select a.id, 
       a.name as article_name, 
       w.name as warehouse_name
from article a
left join inventory i on i.article_id = a.id
left join warehouse w on i.warehouse_id = w.id