I got this table in my MySQL database, \'users\'. It has the fields \'id\' and \'value\'.
Now, I want to update lots of rows in this table with a single
Assuming id is unique or primary...
insert into users
(id,value)
VALUES
(1,53),(2,65),(3,47),(4,53),(5,47)
on duplicate key update
value=VALUES(value)
Rather than doing case variable when value then ...
, try doing case when condition then ...
- like so:
UPDATE users
SET value = CASE
WHEN id in (1,4) THEN 53
WHEN id = 2 THEN 65
WHEN id in (3,5) THEN 47
END
WHERE id IN (1,2,3,4,5)
I would just do this with a few different UPDATE statements.
UPDATE users
SET value = 53
WHERE id = 1;
UPDATE users
SET value = 65
WHERE id = 2;
...
This seems simplest if you only have 5 or 6 values to set on multiple rows each. Or is there some specific reason that you need to do this in one query?