根据相同ID,对字符串列进行聚合【mysql,mssql】

前端之家收集整理的这篇文章主要介绍了根据相同ID,对字符串列进行聚合【mysql,mssql】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

TableA

ID Names

1 n1

2 n2

1 nn2

2 nn3

3 nn4

1 nn1

ID相同的记录,对Names进行聚合

得到结果是:

ID SumNames

1 n1,nn2,nn1

2 n2,nn3

3 nn4

MysqL

select id,group_concat(names) from tableA group by id;

mssql

select stuff((select ','+names from tableA as t2 where t2.id=t1.id for xml path('')),1,1) from (select id,names from tableA as t1 group by id);


select stuff(

(select ','+convert(Varchar(50),MemberGuid) from (SELECT a.MemberGuid,a.managerguid,b.UserName FROM membermanager a LEFT JOIN k_manager b ON a.managerguid = b.managerguid) as t2 where t2. MemberGuid = t1.MemberGuid FOR XML PATH('')),'' ) from (SELECT a.MemberGuid,b.UserName FROM membermanager a LEFT JOIN k_manager b ON a.managerguid = b.managerguid) as t1 where memberguid=@MemberGuid group by memberguid

猜你在找的设计模式相关文章