Skip to content
Advertisement

UPDATE mysql rows

I’m trying to update a lot (close to 500) of rows in mysql database. How can I make it with just 1 query?

UPDATE products SET product_qty=5 WHERE product_id=1,2,3,4,5...

…and it goes on and on until 500.

Advertisement

Answer

Use the BETWEEN operator:

UPDATE products SET product_qty=5 WHERE product_id BETWEEN 1 AND 500

If not all products with an ID in that range should be updated, you’ll have to use the IN operator and construct the query with some PHP code:

UPDATE products SET product_qty=5 WHERE product_id IN (1, 2, 3, 6, 8, 11, ..., 346)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement