What is the proper query for updating multiple rows in MySQL at the same time?
I am only updating 1 column:
UPDATE example_table SET variable1 = 12 WHERE id=1;
UPDATE example_table SET variable1 = 42 WHERE id=2;
UPDATE example_table SET variable1 = 32 WHERE id=3;
UPDATE example_table SET variable1 = 51 WHERE id=4;
This seems like it may be inefficient, or if it is the most efficient query let me know :)
you can use cases like below:
UPDATE example_table
SET variable1 = CASE id
WHEN 1 THEN 12
WHEN 2 THEN 42
WHEN 3 THEN 32
WHEN 4 THEN 51
END
WHERE id BETWEEN 1 AND 4
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With