使用CASE MySQL时出错

时间:2013-06-10 22:47:40

标签: mysql

我遇到了一些代码,并将其修改为新的列名。我已经多次检查拼写问题,但无济于事。

UPDATE `company_playtime` 
SET front_player_count = CASE 
    WHEN (front_paid + 1) > front_player_count THEN (front_paid + 1) 
    ELSE front_player_count 
END;

此代码出错:

错误号码:1054 '字段列表'中的未知列'front_player_count'

我正在使用codeigniter和php,如果有帮助的话。

感谢。

1 个答案:

答案 0 :(得分:5)

为什么不尝试使用GREATEST呢?即。

UPDATE `company_playtime`
SET front_player_count = GREATEST( front_player_count , front_paid + 1 )

或者更好的是,只是不要更新未受影响的行

UPDATE `company_playtime`
SET front_player_count = front_paid + 1
-- use of <= negates requirement for +1 here, should be more efficient
WHERE front_player_count <= front_paid 

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_greatest

这假设所提到的列确实存在于company_playtime