Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL update multiple rows on specific ID's

Tags:

sql

sql-update

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"
like image 596
Tortooga Avatar asked Oct 25 '25 02:10

Tortooga


1 Answers

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)
like image 167
Mureinik Avatar answered Oct 26 '25 16:10

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!