I am trying to write a query to update several rows of my SQL table at once. Below is the code I have tried, and it doesn't appear to be proper SQL as it doesn't work. Is there a way to accomplish this is one query?
$query = "UPDATE table_names
SET Name='Bob' WHERE ID=7
SET Name='Mike' WHERE ID=34"
One way to do this is with a case expression:
UPDATE table_name
SET name = CASE id WHEN 7 THEN 'Bob'
WHEN 34 THEN 'Mike'
END
WHERE id IN (7, 34)
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