日期:2014-05-16  浏览次数:20631 次

mysql select语句
有一表ab内容如下
id name sex year
1 1 2011
2 ddd 0 2012
3 1 2011
4 1 2011
5 ggg 0 2012
6 1 2011
7 fff 1 2011
8 ggg 0 2012
9 1 2011
10 aaa 1 2000
11 1 2011

查找结果为
count(id),group_concat(niu)
11 ,aaa,ddd,,,ggg,,fff,ggg,,aaa,

这是我的语句:select count(id),group_concat(name) from ab
找出的结果里含有太多的逗号了,怎么去掉它啊?
理想的结果为:
count(id),group_concat(name)
11 aaa,ddd,ggg,fff,ggg,,aaa


------解决方案--------------------
有name是空看来


select count(id),group_concat(name) from ab where name is not null
------解决方案--------------------
SQL code

select count(*), substring_index(GROUP_CONCAT(name order by name desc),',,',1) from ab;

------解决方案--------------------
select count(id),group_concat(if(name is null or name='','',concat(name,',')) SEPARATOR '') from ab
------解决方案--------------------
+1
探讨
select count(id),group_concat(if(name is null or name='','',concat(name,',')) SEPARATOR '') from ab