我正在尝试加入3张桌子
物品,库存和售出表
我想为每个商品从表Stock中选择其QteEntree的总和
桌上的Qte已售
这是我的代码
Select Article.IdArt,
Sum(sold.Qte),
Sum(Stock.QteEntree)
from (((Article)
Left Join Sold on Sold.IdArt = Article.IdArt)
Left Join Stock on Stock.IdArt = Article.IdArt)
Group by Article.IdArt
但是我总是得到(例如)IdArt乘以QteEntree的结果乘以2将Qte乘以3
我想是因为我在Sold表中有2条记录,在Stock表中有3条记录
我怎样才能解决这个问题?
谢谢
最佳答案
使用子查询试一下:
select a.IdArt,
(select sum(Qte) from Sold where IdArt = a.IdArt group by IdArt) as QteSum,
(select sum(QteEntree) from Stock where IdArt = a.IdArt group by IdArt) as QteEntreeSum
from Article a
关于sql - 具有聚合功能的多个mysql JOIN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11337355/