SQL一列的合并连起来

        CREATE TABLE #temp(
                ID INT,
               name NVARCHAR(max),
                age int,
                address NVARCHAR(300)
                )
        insert into #temp
        select 
            ID,
            name = 
            (
                stuff(
                    (select ',' + SN from #temp where ID = A.ID and age = A.age for xml path('')),
                    1,
                    1,
                    ''
                )
            ) ,
        age, address
from #temp as A group by A.age,A.ID,A.address drop table #temp

原始数据  :

id name age
address
1 张三 20 北京
2 李四 21 北京
3 王五 20 北京

 

#temp数据结果:

id name age address
1 张三,王五 20 北京
2 李四 21 北京


 

posted @ 2018-01-17 13:51  未风  阅读(213)  评论(0编辑  收藏  举报