tags:

views:

23

answers:

1

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?

+2  A: 

This would be one way, if you don't mind using a subquery:

UPDATE tableA
SET column1 = (
    SELECT sum(column2)
    FROM tableB
    WHERE tableA.coumn3 = tableB.column4);
Lukáš Lalinský
The real question I intended to ask here was http://stackoverflow.com/questions/2765768/how-to-update-a-table-using-a-select-group-by-in-a-second-one-and-itself-as-the-dBut as your answer here is correct I had to create another thread.
Jader Dias