MySQL Update Statement
January 24th, 2008 by Andrew Chen
Yesterday I needed to write an update statement
Update C
Set C.status='Active'
From tableA A
Join tableB B on A.id=B.aid
Join tableC C on B.id=C.bid
Where A.id=1 or A.id=2 or A.id=3
This statement failed on MySQL and didn’t pass the syntax check. This statement is in (MSSQL) T-SQL syntax. In MySQL syntax the above update statement has to be rephrased as follow.
Update C
Join tableB B on B.id=C.bid
Join tableA A on A.id=B.aid
Set C.status='Active'
Where A.id=1 or A.id=2 or A.id=3
You may thing the syntax difference between T-SQL and MySQL is little but in fact MySQL update statement can do more than T-SQL update statements. This is what surprised me. In a MySQL update statement you can update more than one table like this.
Update C
Join tableB B on B.id=C.bid
Join tableA A on A.id=B.aid
Set C.status='Active',
A.status='Active'
Where A.id=1 or A.id=2 or A.id=3
There is no way you can do that in one MS SQL(T-SQL) statement. That’s one nice thing I saw in MySQL that MS SQL doesn’t have.


#1146 - Table ’sqldatabase.C’ doesn’t exist
Maybe this only works at some versions of SQL
thank you for that tidbit of sql join wisdom
[…] tip… there’s a wicked-awesome post that explains how to do updates with a join @ this post. « technical is where all the power is link to mag! […]
Thanks. It works
so useful, thanks..