mysql - Update multiple columns from subquery -
this type of thing has been asked few times before, not quite looking for. need set
2 rows equal different parts of subquery.
i using:
update records set leads=(select count(*) leads_table leads_table.blah=records.blah), earnings=(select sum(amount) leads_table leads_table.blah=records.blah)
the statements simplified...but same subquery don't think should running twice?
i want like...
update records set (leads,earnings)=(select count(*),sum(amount) leads_table leads_table.blah=records.blah)
you can join table in subquery calculations,
update records inner join ( select blah, count(*) totalcount, sum(amount) totalsum leads_table group blah ) b on b.blah = a.blah set a.leads = b.totalcount a.earnings = b.totalsum
Comments
Post a Comment