mysql查询中的CASE函数

时间:2019-02-01 13:52:45

标签: mysql

我无法从以下查询中获得所需的结果。请给我一点帮助吗?谢谢。

$sql = "SELECT id, 
                 CASE 
                    when Rcon = '' then '0',
                    when Rcon = 'x' then '1',
                    when Rcon = '2x' then '2',
                    when Rcon = 'x3' then '2',
                    when Rcon = 'x,x3' then '3',
                    when Rcon = 'x4' then '3' 
                    ELSE Rcon END AS Rcon
FROM mytable";

1 个答案:

答案 0 :(得分:1)

您的CASE语句有问题。

SELECT  id, 
        CASE 
          when Rcon = '' then '0'
          when Rcon = 'x' then '1'
          when Rcon = '2x' then '2'
          when Rcon = 'x3' then '2'
          when Rcon = 'x,x3' then '3'
          when Rcon = 'x4' then '3' 
          ELSE Rcon 
        END AS Rcon
  FROM mytable;

请注意,我已经从,语句的每一行末尾删除了逗号(CASE)。逗号在个案陈述中无效。