MySQL update a joined table
The multi-table UPDATE syntax in MySQL is different from Microsoft SQL Server. You don't need to say which table(s) you're updating, that's implicit in your SET clause.
UPDATE tableA a
JOIN tableB b
ON a.a_id = b.a_id
JOIN tableC c
ON b.b_id = c.b_id
SET b.val = a.val+c.val
WHERE a.val > 10
AND c.val > 10;
There is no FROM clause in MySQL's syntax.
UPDATE with JOIN is not standard SQL, and both MySQL and Microsoft SQL Server have implemented their own ideas as an extension to standard syntax.
You have the ordering of the statements wrong. You can read up on the syntax here (I know, it's pretty hard to read.
UPDATE tableA a
JOIN tableB b
ON a.a_id = b.a_id
JOIN tableC c
ON b.b_id = c.b_id
SET b.val = a.val+c.val
WHERE a.val > 10
AND c.val > 10;
sql fiddle
Another correct construction, which we can use in this situation:
UPDATE T1, T2,
[INNER JOIN | LEFT JOIN] T1 ON T1.C1 = T2.C1
SET T1.C2 = T2.C2,
T2.C3 = expr
WHERE condition
The above example is take from: MySQL UPDATE JOIN.
Reaching for the MySQL 8.0 Reference Manual we will find such a description of multiple-table UPDATE syntax:
UPDATE [LOW_PRIORITY] [IGNORE] table_references
SET assignment_list
[WHERE where_condition]
The table_references clause lists the tables involved in the
join
.
So multiple-table MySQL's syntax doesn't support FROM
, ORDER BY
or LIMIT
clauses as opposed to single-table syntax.