【问题标题】:Different delimiter in group concat组连接中的不同分隔符
【发布时间】:2013-06-24 19:21:27
【问题描述】:

我有一个这样的 sql 查询:-

 REPLACE(
  GROUP_CONCAT( 
   IF( 
    (timediff(delta_ts,creation_ts) > '03:00:00')    
     && (priority='P5') ,bug_id,'')
     ),',,','' )
   AS exceeded_bugs
  from bugs
  ......

我得到的结果:-

超出错误:,3743331,3743332,3743333

我需要不同的分隔符,因为 Group concat 的默认分隔符是“,”。我需要使用空格或“|”分隔错误或“-”符号。

我试过给:-

 REPLACE(
  GROUP_CONCAT( 
   IF( 
    (timediff(delta_ts,creation_ts) > '05:00:00')    
    && (priority='P6') ,bug_id,'')
    ) 
    ,SEPARATOR '-' ) 
   AS exceeded_bugs 
  from bugs
  .....

我收到错误:-

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取在 'SEPARATOR '-' 附近使用的正确语法作为第 1 行的超出错误

请帮助更正使用不同分隔符的组 concat 的 sql 语法。

【问题讨论】:

  • 不要认为分隔符前的逗号是必需的。

标签: mysql replace delimiter group-concat


【解决方案1】:

SEPARATOR 前不要加逗号

首先你没有在group_concat函数中包含separator

第二个你没有用替换功能做任何事情

Take a look here

编辑:

   REPLACE(GROUP_CONCAT( IF( (timediff(delta_ts,creation_ts) > '03:00:00') && (priority='P5') ,bug_id,'') SEPARATOR '-'),',,','' ) as exceeded_bugs

【讨论】:

  • 任何其他类型的组连接方法?
  • 我正在使用替换功能,因为结果显示了额外的 ',,' 逗号而不是错误 ID。
  • 它正在显示结果:-超出错误:--3743334------------------------------ ---
  • select sum( IF( (timediff(delta_ts,creation_ts) > '03:00:00') && (priority='P5') ,1,0)) P5_time_exceeded,REPLACE(GROUP_CONCAT( IF ( (timediff(delta_ts,creation_ts) > '03:00:00') && (priority='P5') ,bug_id,'')),',,','' ) as exceeded_bugs from bugs.product_id= 237 and bugs.resolution ='FIXED' and bugs.creation_ts >='2013-06-16 00:00:00' and bugs.creation_ts
  • *************************** 1. 行 ************ *************** P5_time_exceeded: 3 超出_bugs: ,3743331,3743332,3743333
【解决方案2】:

Samir 的回复是在结果中添加了更多连字符。我稍微修改了查询,得到了准确的结果。

萨米尔的询问:-

 select sum( 
    IF( (timediff(delta_ts,creation_ts) > '03:00:00') 
    && (priority='P5') ,1,0))   P5_time_exceeded,
    REPLACE(GROUP_CONCAT( IF( (timediff(delta_ts,creation_ts) > '03:00:00') 
    && (priority='P5') ,bug_id,'') SEPARATOR '-'),',,','' ) as exceeded_bugs 
    from bugs 
     ......

我得到的结果:-

P5_time_exceeded: 3 超出错误:---3743331-3743332-3743333------------------------- 一组中的 1 行(0.00 秒)

修改后的查询

 select sum( 
   IF( (timediff(delta_ts,creation_ts) > '03:00:00') 
   && (priority='P5') ,1,0)) P5_time_exceeded,
   REPLACE(GROUP_CONCAT( IF( (timediff(delta_ts,creation_ts) > '03:00:00')
   && (priority='P5') ,bug_id,'') SEPARATOR '-'),'---','' ) 
   as exceeded_bugs 
   from bugs 
   where .....

得到了确切的结果:-

P5_time_exceeded: 3 超出错误:3743331-3743332-3743333

我将重复 sybmol 的最小倍数替换为空格 '',这样重复符号就不会出现。

谢谢萨米尔。

【讨论】:

  • 我只是猜测你为什么会得到很多 --- 是因为你有空值并且它与 sperator 连接 - 这就是为什么你有很多 ------------ - ,这意味着您的字段为空 concat 字段为空.....所以我认为您应该给出条件以不选择空值
【解决方案3】:

不确定是否需要更换。

GROUP_CONCAT 应该忽略 NULL 字段,但是当你想忽略一个时,你会输入空的出现。而不是使用 '' 尝试使用 NULL 来代替。

像这样:-

SELECT SUM( IF( (TIMEDIFF(delta_ts,creation_ts) > '03:00:00') && (priority='P5') ,1,0)) AS P5_time_exceeded,
GROUP_CONCAT( IF( (TIMEDIFF(delta_ts,creation_ts) > '03:00:00') && (priority='P5'), bug_id, NULL) SEPARATOR '-') AS exceeded_bugs 
FROM bugs 
WHERE .....

【讨论】:

    猜你喜欢
    • 2018-09-15
    • 2020-08-22
    • 1970-01-01
    • 2016-04-28
    • 2020-09-16
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多