sql分组拼接字段


 


--联查
select n.*,t.Name from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId

--拼接并插入临时表
select aaa.Title,aaa.Content, stuff((select ',' + ttt.Name from (select n.*,t.Name from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId ) as ttt for xml path('')),1,1,'') as field into #T from (
select n.* from News n join Type_News tn on n.Id=tn.NId join Types t on t.Id=tn.TId
) aaa

drop table #T

select * from #T

 


--分组查询
select ROW_NUMBER() over(order by Title) as rowId, * from #t group by Title,content,field

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM