我的讲解步骤:
1:构造初始数据
2:提出问题
3:简单介绍FOR XML PATH
4:解答问题
1.构造初始数据
举出一个经典的学生课程例子,共有学生、课程与学生课程三张表。
表1:Student
| student_id | student_name |
|
1 |
张三 |
|
2 |
李四 |
| 3 |
王五 |
表2:Course
| course_id | course_name |
|
1 |
语言 |
|
2 |
数学 |
| 3 |
英语 |
表3:Student_Course
| student_id |
course_id |
|
1 |
2 |
|
1 |
3 |
| 2 |
1 |
| 2 |
3 |
|
3 |
3 |
脚本:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->create table student
(
student_id int primary key,
student_name nvarchar(50) not null
)
create table course
(
course_id int primary key,
course_name nvarchar(50) not null
)
create table student_course
(
student_id int not null,
course_id int not null,
primary key(student_id,course_id)
)
2.提出问题
写一条SQL语句,查询显示出下列结果:
| student_name |
course_name |
|
张三 |
数学,英语 |
|
李四 |
语言,英语 |
|
王五 |
英语 |
3.简单介绍 FOR XML PATH
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select str(student_id) + ',' + student_name from student for xml path('student')
查询结果:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--><student> 1,张三student>
<student> 2,李四student>
<student> 3,王五student>
student已成为一个xml文件中的结点了,再看看FOR XML PATH('')的效果,针对上述SQL作出修改,
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select str(student_id) + ',' + student_name from student for xml path('')
查询结果:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->1,张三 2,李四 3,王五
看得出来,这个参数自动把我们的查询结果串接在一起了,这下子,要做字符串拼接就很简单了!
4. 解答问题
要查询想要的结果,我们首先用一般的SQL语句,连接三个表之后的结果为:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select a.student_name,b.course_name from student_course c,student a,course b where
c.student_id=a.student_id and c.course_id=b.course_id
查询结果:
| student_name |
course_name |
|
张三 |
数学 |
|
张三 |
英语 |
|
李四 |
语文 |
|
李四 |
英语 |
|
王五 |
英语 |
我们把这个查询结果看作为一个临时表,与自身进行一次连接,再得用FOR XML PATH('')参数来对课程course_name列进行拼接,再得用子查询功能。这样就得到一个每一个学生的所选的所有课程,由于上表会存在同一学生的多条记录,所以需要对最后的结果按学生进行分组,先看看查询语句:
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select student_name,
(select course_name+',' from
(
select student_name,course_name from
(
select a.student_name,b.course_name from stud_course c,student a,course b where c.student_id=a.student_id and c.course_id=b.course_id
) as a
) as b where c.student_name=b.student_name for xml path('')
) as course_name
from
(
select a.student_name,b.course_name from student_course c,student a,course b where c.student_id=a.student_id and c.course_id=b.course_id
) as c group by student_name
查询结果:
| student_name |
course_name |
|
张三 |
数学,英语, |
|
李四 |
语言,英语, |
|
王五 |
英语, |
还有个小问题, course_name后面多出一个,号,最后做一次裁剪,假设上面的SQL语句作为一个子查询 subquery
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select student_name,left(course_name,len(course_name)-1) from (........) as subquery
这样,就可以得出最终的结果!可以看得出来FOR XML PATH('') 参数非常强大!
编辑推荐:
- 使用 SQL的 for xml path来进行字符串拼接03-02
- 基于事务日志还原到时间点03-02
- SQL Server 2008 R2 事务与隔离级别实例讲解03-02
- APE是什么格式 APE文件用什么打开03-02
- ccc.exe是什么进程文件03-02
- 复制03-02
- SQL Server FOR XML PATH 语句的应用03-02
- lsass.exe是什么进程03-02
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- 使用 SQL的 for xml path来进行字符串拼接
使用 SQL的 for xml path来进行字符串拼接
26-03-02 - SQL Server 2008 R2 事务与隔离级别实例讲解
SQL Server 2008 R2 事务与隔离级别实例讲解
26-03-02 - ccc.exe是什么进程文件
ccc.exe是什么进程文件
26-03-02 - 复制
复制
26-03-02 - lsass.exe是什么进程
lsass.exe是什么进程
26-03-02 - 索引及统计信息2
索引及统计信息2
26-03-02 - 找不到gdiplus.dll,gdiplus.dll下载
找不到gdiplus.dll,gdiplus.dll下载
26-03-02 - HDCP是什么,有什么功能
HDCP是什么,有什么功能
26-03-02 - diskgenius怎么合并分区?
diskgenius怎么合并分区?
26-03-02 - 最全面的宝贵数据备份解决方案
最全面的宝贵数据备份解决方案
26-03-02
