mysql update 加if,MySQL更新多个IF条件

时间:2025-03-28 19:02:20

I have a table which has been populated with incorrect data, so I need to switch some numbers around. I'm not sure if this would be the best way to go about it, but I'm thinking about using an UPDATE statement with multiple IF conditions. Something like:

UPDATE

`orders`

SET

`orderPriority` = 1

IF(`orderPriority` = 2)

OR

`orderPriority` = 2

IF(`orderPriority = 3)

OR

`orderPriority` = 3

IF(`orderPriority` = 1);

Obviously this doesn't work, but my SQL skills are lacking here. Any help is appreciated!!!

解决方案

UPDATE orders

SET orderPriority = CASE WHEN orderPriority = 1 THEN 3

WHEN orderPriority = 2 THEN 1

WHEN orderPriority = 3 THEN 2

END

WHERE orderPriority IN (1,2,3)