SQL语句 分组 多行合并成一行

时间:2022-08-20 10:25:57
SELECT  Id, Names = (STUFF((SELECT ', ' + Names FROM Table WHERE Id = d.Id FOR xml path('') ),1,1,'')) FROM Table d GROUP by Id

另外:

sql 单引号转义:两个单引号转义为一个单引号

set @sql='STUFF((SELECT '','' + Names FROM Table WHERE Id = d.Id FOR xml path('''') ),1,1,'''')) FROM Table d GROUP by Id '

'' 为 '