MySql 字段分组拼接


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; #去重且去空字符串

 

 #原表

#直接拼接

 

 #去重,但未去空

 

 #去重且去空

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM