Updating multiple rows with different values

前端 未结 3 2046
忘了有多久
忘了有多久 2020-12-31 19:07

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

3条回答
  •  -上瘾入骨i
    2020-12-31 19:18

    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)
    

提交回复
热议问题