我有一个查询,我似乎无法操作在MySQL中的SUM函数中工作:
这就是我想要的:
更新account_seeds AS a INNER JOIN b AS b ON b.accountID = a.accountID AND a.areaID = b.areaID
INNER JOIN b_seed AS s ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c ON c.seedID = s.seedID
设置a.amount = a.amount + SUM(s.amount)
在哪里b.status ='活跃' AND a.seedID = s.seedID
现在它显然不允许我在更新中使用SUM而不将其分开。我已尝试加入选择查询,但无法理解我的想法。基本前提是我有多个建筑物(行),其种子值将增加特定帐户区域中该类型的总种子数。没有总和,它只更新一个具有匹配种子值的建筑物
答案 0 :(得分:2)
UPDATE
account_seeds AS a
INNER JOIN
( SELECT b.accountID, b.areaID, s.seedID
, SUM(s.amount) AS add_on
FROM b AS b
INNER JOIN b_seed AS s
ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c
ON c.seedID = s.seedID
WHERE b.status = 'active'
GROUP BY b.accountID, b.areaID, s.seedID
) AS g
ON g.accountID = a.accountID
AND g.areaID = a.areaID
AND g.seedID = a.seedID
SET
a.amount = a.amount + g.add_on ;
答案 1 :(得分:1)
也许您可以使用嵌套查询:
UPDATE account_seeds AS a
INNER JOIN b AS b ON b.accountID = a.accountID AND a.areaID = b.areaID
INNER JOIN b_seed AS s ON s.buildingID = b.buildingID
INNER JOIN seed_class AS c ON c.seedID = s.seedID
SET a.amount = a.amount + (SELECT SUM(amount) FROM b_seed)
WHERE b.status='active' AND a.seedID = s.seedID
你能试试吗?