Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL UPDATE Multiple IF Conditions

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!!!

like image 931
csm232s Avatar asked Aug 31 '25 02:08

csm232s


1 Answers

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)
like image 52
Joe Stefanelli Avatar answered Sep 02 '25 17:09

Joe Stefanelli