求SQL 话语写法
求SQL 语句写法
表结构如下:
编号 参数
1 红色
1 36码
2 蓝色
2 37码
2 中国
我想查出这样的效果:
编号 参数
1 红色,36码
2 蓝色,37码,中国
------解决思路----------------------
------解决思路----------------------
create table test(bm int,Parameter varchar(50))
insert into test
select
1 , '红色'
union all select
1 , '36码'
union all select
2 , '蓝色'
union all select
2 , '37码'
union all select
2 , '中国'
select * from test
select bm,STUFF(Parameter,1,1,'') Parameter from (
select bm,(select ','+ Parameter from test
where bm=a.bm for xml path('')) Parameter
from test a ) b
表结构如下:
编号 参数
1 红色
1 36码
2 蓝色
2 37码
2 中国
我想查出这样的效果:
编号 参数
1 红色,36码
2 蓝色,37码,中国
------解决思路----------------------
select 编号,参数=stuff((select ','+参数 from table where 编号=a.编号 for xml path('')),1,1,'') from table a group by 编号
------解决思路----------------------
create table test(bm int,Parameter varchar(50))
insert into test
select
1 , '红色'
union all select
1 , '36码'
union all select
2 , '蓝色'
union all select
2 , '37码'
union all select
2 , '中国'
select * from test
select bm,STUFF(Parameter,1,1,'') Parameter from (
select bm,(select ','+ Parameter from test
where bm=a.bm for xml path('')) Parameter
from test a ) b