我的查询。
UPDATE assets SET assets.Amount = (SELECT SUM(assets.Amount) - NEW.Amount FROM assets WHERE NEW.UserId = assets.UserId and NEW.AccountId = assets.AccountId) AS TmpAssets
WHERE NEW.UserId = assets.UserId and NEW.AccountId = assets.AccountId
最佳答案
MySQL 不允许您在更新
或删除
中使用在子查询中更新的表。解决这个问题很容易。
这是一种使用update
/join
的方法:
UPDATE assets a JOIN
(select sum(a.Amount) as sumamount, a.UserId, a.AccountId
from assets a
where NEW.UserId = a.UserId and NEW.AccountId = a.AccountId
group by a.UserId, a.AccountId
) anew
on NEW.UserId = a.UserId and NEW.AccountId = a.AccountId
SET a.Amount = anew.sumamount - new.Amount;
关于mysql - 无法在 FROM 子句中指定更新目标表 'assets',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26214596/