mysql数据库中如何对字符串列-进行”去重拼接”,”去重剔除空字符串拼接”呢?


摘要:
下文讲述mysql字符串聚合的示例分享,如下所示;

例:

create table tableTest 
  select 'maomao' parent, 'maomao1' child union all
  select 'maomao', 'maomao1' union all
  select 'maomao', '' union all
  select 'maomao', null union all
  select 'maomao', 'maomao2' union all
  select 'maomaoB', 'maomaoB1' union all
  select 'maomaoB', 'maomaoB2' union all
  select 'maomaoC', 'maomaoC1' union all
  select 'maomaoC', 'maomaoC2' ;

select * from tableTest;

/*直接拼接*/
select parent ,
   group_concat(child) as child 
     from tableTest group by parent;   

/*去重,不剔除空字符串*/                  
select parent ,
   group_concat(distinct child) as child 
     from tableTest group by parent;


/*去重,并剔除空字符串*/
select parent , 
   group_concat(distinct nullif(child, '')) as child 
    from tableTest group by parent;