select * from @T1
—–最优的方式
SELECT CityName,STUFF((SELECT ‘,’ + UserName FROM @T1 subTitle WHERE CityName=A.CityName FOR XML PATH(”)),1, 1, ”) AS A
FROM @T1 A
GROUP BY CityName
—-第二种方式
SELECT B.CityName,LEFT(UserList,LEN(UserList)-1)
FROM (
SELECT CityName,(SELECT UserName+’,’ FROM @T1 WHERE CityName=A.CityName FOR XML PATH(”)) AS UserList
FROM @T1 A
GROUP BY CityName
) B
stuff(select ‘,’ + fieldname from tablename for xml path(”)),1,1,”)
© 版权声明
文章版权归作者所有,未经允许请勿转载。