id | name
1 | a
1 | b
2 | c
2 | d
若想要得到以下结果,将相同id的name合并成同一行,并用逗号分开:
1 | a,b
2 | c,d
方法1:
利用函数 wm_concat
select id,wm_concat(',',name) as nn from tmp_test group by id;
格式
string wm_concat(string separator, string str)
说明
用指定的separator连接str中的值
注意
若str中有一个值为null,在有的环境中都返回null(环境不同,用法不同)
方法2:
若无法使用以上函数,也可是另作处理:
利用函数 collect_set
select id,concat_ws(',',collect_set(name)) from tmp_test group by id;