Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
mysql查询中的CASE函数_Mysql - Fatal编程技术网

mysql查询中的CASE函数

mysql查询中的CASE函数,mysql,Mysql,我无法从以下查询中获得所需的结果。能帮我一点忙吗?多谢各位 $sql = "SELECT id, CASE when Rcon = '' then '0', when Rcon = 'x' then '1', when Rcon = '2x' then '2', when Rcon = 'x3' th

我无法从以下查询中获得所需的结果。能帮我一点忙吗?多谢各位

$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";

您的
案例
陈述有问题

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
语句的每一行末尾删除了逗号(
)。逗号在case语句中无效。

在else部分,您想做什么?在else部分,我尝试给出一个选项,如果有任何意外数据。如果您只是读取从mysql引发的错误消息,您可以得到错误的原因。因此,只需逗号就可以了?谢谢你,马丁。这管用@Max是的,只是逗号-在
情况下不需要它们
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;