How can I update multiple rows in one query?
UPDATE table SET col_1=col_1+5, col_2=col_2+7 WHERE id=12;
UPDATE table SET col_1=col_1+2, col_2=col_2+6 WHERE id=16
...
UPDATE table SET col_1=col_1+..., col_2=col_2+... WHERE id=...
How to union following queries
2
Answers
Here’s a way to do it:
WITH
and theVALUES
statement requires MySQL 8.0.Demo: https://dbfiddle.uk/DQ_g6CZg
If this is about considering the updates as an atomic action that shall be committed or rolled back, then wrap them in a transaction:
If this is about avoiding round trips by sending one combined statement to the server instead of single statements, one after the other, then combine them: