本文介绍了MySQL更新多个IF条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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);

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

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)

这篇关于MySQL更新多个IF条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 20:56