I can do this:
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
GROUP BY t3.somekey
But how to do this?
UPDATE tableA t1
SET speed = (
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY t3.somekey
)
;
MySQL says it's illegal since you can't specify target table t1
for update in FROM clause.
You are using
MySQL
's extension toGROUP BY
which should not be used in this context.Given the following values:
this subquery:
will return you either
8
or9
: it will calculateSUM(b.value) = 7
and then add a random value fromA
for the given key.For this sample data, which value do you want the speed to be updated to?
You can do it by rewriting your query: