求SQL 语句写法

yinrongg 2015-10-14 09:51:14
表结构如下:
编号 参数
1 红色
1 36码
2 蓝色
2 37码
2 中国

我想查出这样的效果:
编号 参数
1 红色,36码
2 蓝色,37码,中国
...全文
159 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinger126 2015-10-14
  • 打赏
  • 举报
回复
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
qq_17482963 2015-10-14
  • 打赏
  • 举报
回复

select 编号,参数=stuff((select ','+参数 from table where 编号=a.编号 for xml path('')),1,1,'') from table a group by 编号

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧