How to update a table using a select group by in a second one as the data source in MySQL?

Posted by Jader Dias on Stack Overflow See other posts from Stack Overflow or by Jader Dias
Published on 2010-04-29T20:48:29Z Indexed on 2010/04/29 20:57 UTC
Read the original article Hit count: 308

Filed under:
|

I can't do this in MySQL

UPDATE tableA, tableB
SET tableA.column1 = SUM(tableB.column2)
WHERE tableA.column3 = tableB.column4
GROUP BY tableB.column4
;

Neither can I

UPDATE tableA, 
(
  SELECT SUM(tableB.column2) sumB, tableB.column4
  FROM tableB
  GROUP BY tableB.column4
) t1
SET tableA.column1 = sumB
WHERE tableA.column3 = column4
;

Besides it being illegal code, I think you can understand what I tried to do with the queries above. Both of them had the same intent.

How can I do that in MySQL?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about update