MySQL – UPDATE with JOIN

The syntax for cross-table update in MySQL is somewhat different than T-SQL. Personally, the way it’s done in MySQL makes more sense.
MySQL:
UPDATE table1 t1 JOIN table2 t2 ON t1.id = t2.id
SET t1.col1 = t2.col2, t1.col2 = t2.col2
WHERE t2.col2 = 1 AND t1.col1 IS NULL;
T-SQL:
UPDATE table1
SET col1 = t2.col1, col2 = t2.col2
FROM table1 t1 JOIN table2 t2 ON t1.id = t2.id
WHERE t2.col2 = 1 AND t1.col1 IS NULL;
Farhamdani

Sharing insights on tech, blogging, and passive income. Follow for more at farhamdani.eu.org!

Drop your comments, but make sure they’re related to the discussion!

I'd be grateful if you could read the Commenting Rules on this blog before posting a comment.

Post a Comment (0)
Previous Post Next Post