mysql group_concat为空字段添加分隔符

zgo*_*mez 4 mysql sql group-concat

这里有一个示例架构: http://sqlfiddle.com/#! 2/c0723a/2

查询是select id,group_concat(val) from test group by id

结果是

ID GROUP_CONCAT(VAL)
1 ,64,66,,203,214,204

我想连接 val 字段而不用逗号来表示这样的空记录

ID GROUP_CONCAT(VAL)
1 64,66,203,214,204

Vig*_*r A 5

只需使用替换

select id,replace(group_concat(val),',,',',') from test group by id
Run Code Online (Sandbox Code Playgroud)

或者你可以使用IF

select id,group_concat(if(val ='',null, val)) from test group by id
Run Code Online (Sandbox Code Playgroud)

或者你可以使用NULLIF

select id,group_concat(Nullif(val,'')) from test group by id
Run Code Online (Sandbox Code Playgroud)

小提琴演示