SQL字符串合并
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL字符串合并相关的知识,希望对你有一定的参考价值。
create table #tb(id int, value varchar(10),cname varchar(20))
insert into #tb values(1, ‘aa‘,‘aaaa‘)
insert into #tb values(1, ‘bb‘,‘eeee‘)
insert into #tb values(1, ‘aa‘,‘tttt‘)
insert into #tb values(2, ‘aaa‘,‘gggg‘)
insert into #tb values(2, ‘bbb‘,null)
insert into #tb values(2, ‘ccc‘,‘hhhh‘)
insert into #tb values(1, ‘cc‘,‘llll‘)
go
----
---去重 select id, [value] = stuff((select distinct ‘,‘ + [value] from #tb t where id = #tb.id for xml path(‘‘)) , 1 , 1 , ‘‘),max(isnull(cname,‘‘)) from #tb group by id ---不去重 select id, [value] = stuff((select ‘,‘ + [value] from #tb t where id = #tb.id for xml path(‘‘)) , 1 , 1 , ‘‘),max(isnull(cname,‘‘)) from #tb group by id
drop table #tb
本文出自 “bamboo” 博客,请务必保留此出处http://hdf007.blog.51cto.com/42396/1920683
以上是关于SQL字符串合并的主要内容,如果未能解决你的问题,请参考以下文章