22,207
社区成员
发帖
与我相关
我的任务
分享
create table 表A
(ID int,STID int,tag int)
insert into 表A
select 1,1,11 union all
select 2,1,2 union all
select 3,2,23 union all
select 4,3,12 union all
select 5,3,11
select STID,
tag=stuff((select ','+rtrim(b.tag)
from 表A b
where b.STID=a.STID
for xml path('')),1,1,'')
from 表A a
group by STID
order by STID
/*
STID tag
----------- -----------
1 11,2
2 23
3 12,11
(3 row(s) affected)
*/