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