csm232s
csm232s

Reputation: 1660

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

Upvotes: 4

Views: 7479

Answers (2)

Wolvyreen
Wolvyreen

Reputation: 41

Yes, but what if you want to perform multiple conditional checks??

SET orderPriority = CASE WHEN ((field1 = 1) && (field2 = 2)) THEN 4

Upvotes: 4

Joe Stefanelli
Joe Stefanelli

Reputation: 135799

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)

Upvotes: 10

Related Questions