create table tb ([id] int,[name] varchar(2))
insert into tb
select 1,'aa' union all
select 2,'bb' union all
select 1,'cc' union all
select 3,'dd' union all
select 2,'ee'
select * from tb
--查询
select
id,
name=stuff((select ','+name from tb where id=t.id for xml path('')),1,1,'')
from
tb t
group by
id
方法二:
CREATE FUNCTION GET_STRING(@ID INT)
RETURNS VARCHAR(50)
AS
BEGIN
DECLARE @NAME VARCHAR(500)
SELECT @NAME=ISNULL(@NAME+',','')+NAME FROM TB WHERE ID=@ID
RETURN @NAME
END
SELECT ID ,DBO.GET_STRING(ID)NAME FROM TB GROUP BY ID