Can Someone please help me where is the issue am getting 'invalid use of group function' in below query
UPDATE t1
JOIN t2 ON t1.id=t2.id
SET t1.total_amount = SUM(IF((t2.`due` <= 0), t2.`amount`, 0))
WHERE t2.flag=1 AND t2.id=003;
Can Someone please help me where is the issue am getting 'invalid use of group function' in below query
UPDATE t1
JOIN t2 ON t1.id=t2.id
SET t1.total_amount = SUM(IF((t2.`due` <= 0), t2.`amount`, 0))
WHERE t2.flag=1 AND t2.id=003;
You want to aggregate in a subquery, then join and set in the outer query:
update t1
inner join (
select id, sum(case when due < 0 then t2.amount else 0 end) total_amount
from t2
where id = 3 and flag = 1
group by id
) t2 on t1.id = t2.id
set t1.total_amount = t2.total_amount