MySQL UPDATE多个IF条件

时间:2011-03-10 21:07:28

标签: mysql if-statement

我有一个填充了错误数据的表,所以我需要切换一些数字。我不确定这是否是最好的方法,但我正在考虑使用具有多个IF条件的UPDATE语句。类似的东西:

UPDATE 
    `orders`
SET 
    `orderPriority` = 1
    IF(`orderPriority` = 2)
OR
    `orderPriority` = 2
    IF(`orderPriority = 3)
OR
    `orderPriority` = 3
    IF(`orderPriority` = 1);

显然这不起作用,但我的SQL技能缺乏。任何帮助表示赞赏!!!

2 个答案:

答案 0 :(得分:6)

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)

答案 1 :(得分:3)

是的,但如果你想进行多项条件检查会怎样?

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