Updating multiple rows with mySQL

I have a table that looks like this:

property propertyid value active 1 1 datastore 2 apc 

How would I formulate an SQL query to update multiple rows without updating all of them. IE, if there were 6 lines, update 2,3 and 4, but not 1,5 and 6?

Thanks.

+4
source share
1 answer

If it is the same column (s) that you update on all rows with the same value, you can easily do this with such a query.

 UPDATE property SET value=5 where propertyid IN(2,3,4) 

This will set the value 5 to all lines where the property identifier is 2.3 or 4.

If you want to update different rows with different values, I'm afraid you will have to write separate SQL queries. You could come up with an SQL query using the CASE statement, but it would be easy to read and maintain with a simple SQL query.

Assuming you are manipulating a database from an application, I am sure that with any programming language that you will use, it will be as simple as writing 1 SQL statement and a loop, replacing values ​​and executing a query OR adding all the SQL update statements for the string ( while looping) and pass it to the database to do it all at once. I'm sorry that I don’t know if this will have a significant impact on performance, but I believe that doing all at once will have some benefit in terms of performance.

+9
source

All Articles