数据源
user
name age
小红 18
小明 18
小芳 19
------------------------------------------------------------------------------------------
– 默认逗号连接
select u.age,group_concat(u.name) as NameStr from user u group by u.age;
预览:
age NameStr
18 小红,小明
19 小芳
------------------------------------------------------------------------------------------
– separator指定连接符
select u.age,group_concat(u.name separator ‘|’) as NameStr from user u group by u.age;
预览:
age NameStr
18 小红|小明
19 小芳
------------------------------------------------------------------------------------------
– 排序连接
select u.age,group_concat(u.name order by t.name desc) as NameStr from user u group by u.age;
会根据 名字排序 ,升序 去掉 desc