Mysql 组concat中的不同分隔符
我有这样一个sql查询:-Mysql 组concat中的不同分隔符,mysql,replace,delimiter,group-concat,Mysql,Replace,Delimiter,Group Concat,我有这样一个sql查询:- REPLACE( GROUP_CONCAT( IF( (timediff(delta_ts,creation_ts) > '03:00:00') && (priority='P5') ,bug_id,'') ),',,','' ) AS exceeded_bugs from bugs ...... SELECT SUM( IF( (TIMEDIFF(delta_ts,creati
REPLACE(
GROUP_CONCAT(
IF(
(timediff(delta_ts,creation_ts) > '03:00:00')
&& (priority='P5') ,bug_id,'')
),',,','' )
AS exceeded_bugs
from bugs
......
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 .....
我得到的结果是:-
exceeded_bugs: ,3743331,3743332,3743333
P5_time_exceeded: 3
exceeded_bugs: ---3743331-3743332-3743333--------------------------
1 row in set (0.00 sec)
我得到一个错误:-
您的SQL语法有错误;检查与MySQL服务器版本对应的手册,以了解在第1行的“SEPARATOR”-)附近使用的正确语法
请使用不同的分隔符帮助更正组concat的sql语法。不要在分隔符之前使用逗号 首先,您没有在
group\u concat
函数中包含separator
第二,您没有使用replace函数制作任何东西
编辑:
萨米尔的回答是在结果中添加了更多的连字符。我稍微修改了查询,得到了准确的结果 萨米尔的问题:-
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
......
结果我得到:-
exceeded_bugs: ,3743331,3743332,3743333
P5_time_exceeded: 3
exceeded_bugs: ---3743331-3743332-3743333--------------------------
1 row in set (0.00 sec)
得到了确切的结果:-
P5_time_exceeded: 3
exceeded_bugs: 3743331-3743332-3743333
P5_时间_超过:3
超出错误:3743331-3743332-3743333
我正在替换重复sybmol的最小倍数,,,替换为空格“”,以便重复符号不会出现
谢谢萨米尔。不确定您是否需要更换 GROUP_CONCAT应该忽略空字段,但是当您想要忽略一个字段时,您正在输入空的Occurance。请尝试使用NULL,而不是使用“” 像这样:-
REPLACE(
GROUP_CONCAT(
IF(
(timediff(delta_ts,creation_ts) > '03:00:00')
&& (priority='P5') ,bug_id,'')
),',,','' )
AS exceeded_bugs
from bugs
......
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 .....
不要认为分隔符前的逗号是必需的。还有其他类型的组concat方法吗?我正在使用replace函数,因为结果显示的是额外的“,”逗号,而不是bug ID。它显示的结果:-超出了bug:--3743334----------------------选择sum(如果((timediff(delta,creation)>'03:00:00')&(priority='P5')),1,0))超过P5时间,替换(组CONCAT(IF((timediff(delta_ts,creation_ts)>'03:00:00')和&(priority='P5'),bug_id',),,,,,,,,,,,,,'')为bugs.product_id=237和bugs.resolution='FIXED'和bugs.creation_ts>='2013-06-16 00:00:00'和bugs.creation_ts中超过的bugs。第5行******************************P5_时间_超过:3个超过_错误:,37433313433323743333我只是猜测一下为什么会得到这么多——因为你有空值,它与sperator连接在一起——这就是为什么会有很多-----------,这意味着你有空的concat字段。。。。。所以我认为你们应该给出一个条件,不要选择空值