SELECT B.id ,
LEFT(tempname, LEN(tempname) - 1) AS name
FROM ( SELECT id ,
( SELECT name + ','
FROM tablename
WHERE id = A.id
FOR
XML PATH('')
) AS tempname
FROM tablename A
GROUP BY id
) B
转载于:https://www.cnblogs.com/hgblog/p/4487629.html