表A
ID STID tag
1 1 11
2 1 2
3 2 23
4 3 12
5 3 11
... ... ...
查询后
STID num
1 11,2
2 23
3 12,11
3 个解决方案
#1
查询后 的表的第二个字段应该是tag 手误
#2
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)
*/
#3
感谢,我配合自己的表试一下
#1
查询后 的表的第二个字段应该是tag 手误
#2
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)
*/
#3
感谢,我配合自己的表试一下