如何在mySQL中使用嵌套案例?

时间:2018-02-16 09:43:34

标签: mysql if-statement nested group-concat

我想处理嵌套案例:

SELECT *, 
    GROUP_CONCAT(DISTINCT CONCAT(
    CASE
      WHEN data.name NOT "field" THEN   
        CASE
          WHEN data.category = "A" THEN "A"
          WHEN data.category = "B" THEN "B"
          ELSE "C"
        END,data.name
    END
   )ORDER BY FIND_IN_SET(data.category,"B,C,A") ASC, data.name ASC SEPARATOR " <br>") AS result
  FROM data;');

但是我收到一条错误消息:

  

致命错误:未捕获PDOException:SQLSTATE [42000]:语法错误或   访问冲突:1064您的SQL语法有错误;检查   手册,对应右边的MySQL服务器版本   在&#39; data.name END&#39;附近使用的语法在第11行

1 个答案:

答案 0 :(得分:1)

您的查询中存在一些语法错误。在查询编辑器中运行以下代码,它就可以了。

SELECT 
 *,
GROUP_CONCAT(
DISTINCT CONCAT(
  CASE
    WHEN data.name = "field" 
    THEN (
      CASE
        WHEN data.category = "A" 
        THEN "A" 
        WHEN data.category = "B" 
        THEN "B" 
        ELSE "C" 
      END
    ) 
  END,
    data.name
) 
ORDER BY FIND_IN_SET(data.category, "B,C,A") ASC,
data.name ASC SEPARATOR " <br>"
) AS result 
 FROM DATA;