数据库语句合并

go
create table Student
(
ID int not null,
Name nvarchar(50) not null,
Type nvarchar(50) null,


constraint Pk_ID primary key(ID)
)
select * from dbo.Student

insert into dbo.Student values(1,'学生A','高个子')
insert into dbo.Student values(2,'学生A','胖子')
insert into dbo.Student values(3,'学生B','女生')
insert into dbo.Student values(4,'学生A','男生')
insert into dbo.Student values(5,'学生B','校花')

select Name, stuff((select ','+Type from Student where Name='学生A' for xml path('')),1,1,'') as 'Type'  from Student group by Name having Name='学生A'

得到结果如下:

Name    Type

学生A    高个子,胖子,男生

你可能感兴趣的:(数据库语句合并)