将多行转换为以逗号作为分隔符的一行

将多行转换为以逗号作为分隔符的一行

如果我发布SELECT username FROM Users我得到这个结果:

username
--------
Paul
John
Mary

但我真正需要的是用逗号分隔所有值的行,如下所示:

Paul, John, Mary

我该怎么做?


牛魔王的故事
浏览 390回答 3
3回答

摇曳的蔷薇

这应该对你有用。测试了所有返回SQL 2000的过程。create table #user (username varchar(25))insert into #user (username) values ('Paul')insert into #user (username) values ('John')insert into #user (username) values ('Mary')declare @tmp varchar(250)SET @tmp = ''select @tmp = @tmp + username + ', ' from #userselect SUBSTRING(@tmp, 0, LEN(@tmp))

神不在的星期二

 select    distinct       stuff((         select ',' + u.username                 from users u                  where u.username = username                   order by u.username                      for xml path('')     ),1,1,'') as userlistfrom usersgroup by username以前有一个错误,上面的作品
打开App,查看更多内容
随时随地看视频慕课网APP