Reputation: 79
I have this update query:
UPDATE
account INNER JOIN c1
ON c1.user = account.u_id
SET
account.amount = account.amount + sum(c1.total_1)
WHERE
t_ype =8
but it returns Invalid Use Of Group Function... where is my mistake?
Upvotes: 1
Views: 69
Reputation: 204784
UPDATE account
JOIN
(
select user, sum(total_1) as sum_total
from c1
group by user
) c1 ON c1.user = account.u_id
SET account.amount = account.amount + sum_total
WHERE account.t_ype = 8
Upvotes: 1
Reputation: 1270021
You need to do the aggregation before the join
:
UPDATE account INNER JOIN
(SELECT c1.user, sum(c1.total_1) as sumc
FROM c1
GROUP BY c1.user
) c1
ON c1.user = account.u_id
SET account.amount = account.amount + sumc
WHERE account.t_ype = 8 ;
Upvotes: 1