SQL 中合并某一列中的数据

create table tb(id int, value varchar(10)) 
insert into tb values(1, 'aa') 
insert into tb values(1, 'bb') 
insert into tb values(2, 'aaa') 
insert into tb values(2, 'bbb') 
insert into tb values(2, 'ccc') 
go 
 
select id, [values]=stuff((select ','+[value] from tb t where id=tb.id 
for xml path('')), 1, 1, '') 
from tb 
group by id 
 
/* 
id          values 
----------- -------------------- 
1          aa,bb 
2          aaa,bbb,ccc 
 
(2 row(s) affected) 
 
*/ 
 
drop table tb 

posted @ 2011-12-09 16:49  小师傅  阅读(462)  评论(0编辑  收藏  举报