drop table if exists T_Test;
create table T_Test 
  select 'A' parent, 'A1' child union all
  select 'A', 'A1' union all
  select 'A', '' union all
  select 'A', null union all
  select 'A', 'A2' union all
  select 'B', 'B1' union all
  select 'B', 'B2' union all
  select 'C', 'C1' union all
  select 'C', 'C2' ;

select * from t_test;
select parent , group_concat(child) as child from t_test group by parent;                      #直接拼接
select parent , group_concat(distinct child) as child from t_test group by parent;             #去重 但未去空字符串
select parent , group_concat(distinct nullif(child, '')) as child from t_test group by parent; #去重且去空字符串

 

 #原表

MySql 字段分组拼接

#直接拼接

MySql 字段分组拼接

 

 #去重,但未去空

MySql 字段分组拼接

 

 #去重且去空

MySql 字段分组拼接

 

相关文章: