SQL Server中将多行数据拼接为一行数据(一个字符串)
- 2023-08-21
- 237 人已阅读
第一种,不使用stuff,结果如下:
select id, [val]=( select [value] +',' from tb as b where b.id = a.id for xml path('') ) from tb as a group by id
第二种,使用stuff将最后的逗号去掉:
select id, [val]=stuff(( select ','+[value] from tb as b where b.id = a.id for xml path('')),1,1,'')from tb as a group by id