mySQL Group_Concat 和查询出错的情况

mySQL Group_Concat and Case when query gives error

我收到错误消息“调用本机函数时参数不正确 'CONCAT':在下面的查询中:

SELECT
  *,
  GROUP_CONCAT(
    CASE
      WHEN `REASONORINSTRUCTIONCODE` = 'R'
      THEN CONCAT(
        "name-",
        `USERWHOENTEREDTHISLINE`,
        ",reason-",
        RTRIM(
          `REASONSORSHIPPINGINSTRUCTIONS`
        )
      END,
      ", "
    )
  ) AS reason,
  GROUP_CONCAT(
    CASE
      WHEN [ `REASONORINSTRUCTIONCODE` ] = 'S'
      THEN CONCAT(
        "name-",
        `USERWHOENTEREDTHISLINE`,
        ",shipping instruction-",
        RTRIM(
          `REASONSORSHIPPINGINSTRUCTIONS`
        )
      END,
      ", "
    )
  ) AS shipping instruction
FROM
  TABLE
GROUP BY `PICKUP_NO`

您的查询有几个问题。首先,您没有以结尾 ) 结束 CONCAT,接下来您的 AS shipping instruction 不能包含 space。接下来,您有 [REASONORINSTRUCTIONCODE],删除 []

看看下面的格式化查询:

SELECT
  *,
  GROUP_CONCAT(
    CASE
      WHEN `REASONORINSTRUCTIONCODE` = 'R'
      THEN CONCAT(
        "name-",
        `USERWHOENTEREDTHISLINE`,
        ",reason-",
        RTRIM(
          `REASONSORSHIPPINGINSTRUCTIONS`
        ))
      END,
      ", "
    )
   AS reason,
  GROUP_CONCAT(
    CASE
      WHEN `REASONORINSTRUCTIONCODE` = 'S'
      THEN CONCAT(
        "name-",
        `USERWHOENTEREDTHISLINE`,
        ",shipping instruction-",
        RTRIM(
          `REASONSORSHIPPINGINSTRUCTIONS`
        ))
      END,
      ", "
    )
   AS shipping_instruction
FROM
  `TABLE`
GROUP BY `PICKUP_NO`