表样:
sqlserver:
--方法1
DECLARE @STR VARCHAR(8000)
SELECT @STR=ISNULL(@STR+',','')+userID FROM (SELECT DISTINCT userID FROM Users)AS T
SELECT @STR AS NAME
结果:
--方法2
declare @output varchar(8000)
select @output = coalesce(@output + ',' , '') + userid from Users
print @output
结果:
![](https://img-blog.csdn.net/20151230211223606?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
mysql:
SELECT GROUP_CONCAT(userID) as id from Users
查询结果: