表样:
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
结果:
mysql:
SELECT GROUP_CONCAT(userID) as id from Users
查询结果: