MYSQL:JOIN使SUM加倍

| 这应该真的很容易。相信我,我已经研究了几个小时。我的查询:
SELECT not_piece.pid, part.name AS \'Part Name\', SUM(qty_left) AS \'In Stock\'
FROM not_piece 
JOIN part ON not_piece.pid = part.pid 
GROUP BY part.name;
只有两个表,not_piece和part。
select qty_left 
from not_piece 
where pid=\'M-MP-007r8\'; 
返回5。 由于part.name在部件表中出现两次(没关系),所以总和为10,而不是5。 如何在不加倍总和的情况下加入此联接? 谢谢。     
已邀请:
SELECT t.pid, p.name AS \'Part Name\', t.InStock AS \'In Stock\'
    FROM (SELECT pid, SUM(qty_left) AS InStock
              FROM not_piece
              GROUP BY pid) t
        INNER JOIN part p
            ON t.pid = p.pid
    
使用GROUP BY,尝试按主键pid进行分组:
SELECT not_piece.pid, part.name AS \'Part Name\', SUM(qty_left) AS \'In Stock\'
FROM not_piece 
JOIN part ON not_piece.pid = part.pid 
GROUP BY part.pid;
    

要回复问题请先登录注册