<p>来自:<a href="http://topic.csdn.net/u/20100701/15/fd028ff8-20b9-4902-a77a-08d6c4f55cf1.html">http://topic.csdn.net/u/20100701/15/fd028ff8-20b9-4902-a77a-08d6c4f55cf1.html</a></p>

 问:

 

SQL code
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
 
结果
 
姓名 选择课程
张三 语文,数学
李四 语文,数学,物理


以上可以用一句SQL实现吗???

 

答:

能! 

 

SQL code
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
 
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)
select 姓名,
选择课程=stuff((select ','+课程 from TB where 姓名=t.姓名 for xml path('')),1,1,'')
from tb t
group by 姓名
/*
姓名 选择课程
---------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
李四 语文,数学,物理
张三 语文,数学
 
(2 行受影响)
 
*/
 
drop table TB

 

 

 

  

另附:

 合并列值
原著:邹建
改编:爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-12-16 广东深圳

表结构,数据如下:
id value
----- ------
1 aa
1 bb
2 aaa
2 bbb
2 ccc

需要得到结果:
id values
------ -----------
1 aa,bb
2 aaa,bbb,ccc
即:group by id, 求 value 的和(字符串相加)

1. 旧的解决方法(在sql server 2000中只能用函数解决。)
--1. 创建处理函数
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

CREATE FUNCTION dbo.f_str(@id int)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @r varchar(8000)
SET @r = ''
SELECT @r = @r + ',' + value FROM tb WHERE [email protected]
RETURN STUFF(@r, 1, 1, '')
END
GO

-- 调用函数
SELECt id, value = dbo.f_str(id) FROM tb GROUP BY id

drop table tb
drop function dbo.f_str

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

--2、另外一种函数.
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

--创建一个合并的函数
create function f_hb(@id int)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return(@str)
End
go

--调用自定义函数得到结果:
select distinct id ,dbo.f_hb(id) as value from tb

drop table tb
drop function dbo.f_hb

/*
id value
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
(所影响的行数为 2 行)
*/

2. 新的解决方法(在sql server 2005中用OUTER APPLY等解决。)
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go
-- 查询处理
SELECT * FROM(SELECT DISTINCT id FROM tb)A OUTER APPLY(
SELECT [values]= STUFF(REPLACE(REPLACE(
(
SELECT value FROM tb N
WHERE id = A.id
FOR XML AUTO
), ' <N value="', ','), '"/>', ''), 1, 1, '')
)N
drop table tb

/*
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc

(2 行受影响)
*/

--SQL2005中的方法2
create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

select id, [values]=stuff((select ','+[value] from tb t where id=tb.id for xml path('')), 1, 1, '')
from tb
group by id

/*
id values
----------- --------------------
1 aa,bb
2 aaa,bbb,ccc

(2 row(s) affected)

*/

drop table tb



create table [user](
id int,name varchar(20)
)
go
insert [user] select
1, '张三'
union all select
2, '李四'
union all select
3, '王五'
go
create table skill (
id int, uid int, name varchar(20), level varchar(20)
)
insert skill select
1, 1, '英语', '6级'
union all select
2, 3, '日语', '初级'
union all select
3, 1, '法语', '入门'
union all select
4, 3, '英语', '4级'
union all select
5, 3, '德语', '精通'

go

create function fn_skill(
@uid int,
@name varchar(20)
)
returns varchar(500)
as
begin
declare @r varchar(500)
if @name = '' ---空表示查所有
select @r = isnull(@r + '、','') + name from skill where [email protected]
else
select @r = isnull(@r + '、','') + name from skill where [email protected] and name = @name
return @r
end
go

create function fn_skilldetail(
@uid int,
@name varchar(20)
)
returns varchar(500)
as
begin
declare @r varchar(500)
if @name = '' ---空表示查所有
select @r = isnull(@r + '、','') + name + '-' + level from skill where [email protected]
else
select @r = isnull(@r + '、','') + name + '-' + level from skill where [email protected] and name = @name
return @r
end
go

--1
select id as uid,name,dbo.fn_skill(id,'') as skill
from [user]
go
--2
select id as uid,name,dbo.fn_skilldetail(id,'') as skill
from [user]

--3
select id as uid,name,dbo.fn_skilldetail(id,'英语') as skill
from [user]

--4
select id as uid,name,dbo.fn_skilldetail(id,'英语') as skill
from [user]
where dbo.fn_skilldetail(id,'英语') is not null

--结果
uid name skill
----------- -------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 张三 英语、法语
2 李四 NULL
3 王五 日语、英语、德语

(3 行受影响)

uid name skill
----------- -------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 张三 英语-6级、法语-入门
2 李四 NULL
3 王五 日语-初级、英语-4级、德语-精通

(3 行受影响)

uid name skill
----------- -------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 张三 英语-6级
2 李四 NULL
3 王五 英语-4级

(3 行受影响)

uid name skill
----------- -------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1 张三 英语-6级
3 王五 英语-4级

(2 行受影响)