首页 > 数据库技术 > 详细

SQL字符串合并

时间:2017-04-29 16:04:20      阅读:222      评论:0      收藏:0      [点我收藏+]

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字符串合并

原文:http://hdf007.blog.51cto.com/42396/1920683

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!