SQL嵌套PPT课件_第1页
SQL嵌套PPT课件_第2页
SQL嵌套PPT课件_第3页
SQL嵌套PPT课件_第4页
SQL嵌套PPT课件_第5页
已阅读5页,还剩29页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、.,1,Student表,.,2,SC表,.,3,Course表,.,4,嵌 套 查 询,问题的引入 试查询与”刘晨“同学在同一个系的学生学号、姓名。 分步完成:1) 确定”刘晨“所在的系 select sdept from student where sname=“刘晨” 查询结果为:计算机 2)查询转换为 查询计算机系学生的学号、姓名,.,5,2) select sno,sname from student where sdept=计算机 可以用一次SQL查询完成吗? select sno,sname from student where sdept in ( select sdept f

2、rom student where sname=刘晨),.,6,一、带in谓词的嵌套查询 谓词in是连接父子查询最常用的谓词。某些 情况下,in可以用=代替。 select sno,sname from student where sdept in ( select sdept from student where sname= 刘晨 ) 定义:一个select-from-where语句称为一个查询块。将一个查询块嵌套在另一个查询块的where或having子句中的查询称为嵌套查询。,内查询块、子查询,外层查询块、父查询,谓词in进行父、子查询间的连接,.,7,思考:系统对嵌套语句执行先后顺序

3、是怎样的? 由里向外进行处理,将子查询的查询结果作为父 查询的查询条件。 select sno,sname select sno,sname from student from student where sdept in where sdept in (计算机) ( select sdept from student where sname= 刘晨),.,8,例1、 查询选修了课程名为”信息系统“的学生学号。 第一步:找到”信息系统“的课程号 select cno from course where cname=信息系统 结果为: cno 3 第二步:找到选修了cno=3 的学生学号 sel

4、ect sno from sc where cno=3,.,9,构建嵌套查询: select sno from sc where cno in ( select cno from course where cname=信息系统) 注意 :该查询只有一层嵌套。SQL嵌套查询允许多层嵌套格式。,找到”信息系统“的课程号,结果为cno=3,1,2,找到选修了cno=3的学生学号,.,10,例2、 查询选修了课程名为”信息系统“的学生姓名。 1)找到”信息系统“的cno select cno from course where cname=信息系统 结果cno=3 2)找到选修了cno=3的学生学号

5、select sno from sc where cno=3 结果为: sno : 200215121 200215122。,.,11,3)找到学号为200215122、200215121的姓名。 select sname from student where sno=200215122 or sno=200215121 思考:该查询会构建成几层嵌套?,.,12,select sname from student where sno in (select sno from sc where cno in (select cno from course where cname=信息系统),1,2

6、,3,.,13,二、带比较运算符的嵌套查询 当子查询返回的是单值时,父查询和子查询之 间需要进行值的比较时,用比较运算符进行连接。 = (select sage from student where sname=张立),子查询的范返回值是一个确定的值,.,14,例2、查询成绩小于2号课程的平均成绩的 课程号。 select distinct cno from sc where grade 5),.,25,7、课程平均成绩大于80分的任课教师名。 - select tname - from teacher - where tno in - (select tno - from course -

7、where cno in - (select cno - from score - group by cno - having avg(grade)80) 8、查询95031班同学所选各门课程的任课教师名。 select tname from teacher where tno in (select tno from course where cno in (select distinct cno from student,score where student.sno=score.sno and class=95031),.,26,三、带有any或all谓词的子查询 当子查询返回多值时,需用

8、any或all谓词修饰比较运算符 = any 仅大于子查询结果集中的某一个值 min =any 仅大于等于子查询结果集中的某一个值 any 仅小于子查询结果集中的某一个值 max =all 大于等于子查询结果集中的所有值 all 小于子查询结果集中的所有值 min =any 小于等于子查询结果集中的所有值 !=any 不等于子查询结果中的某个值 !=all 不等于子查询结果集中的任何值,.,28,例1:查询其他系中仅比计算机某一学生年 龄小的学生姓名和年龄。 仅比计算机某一学生年龄小 any max select sname,sage from student where sageany (s

9、elect sage from student where sdept=计算机) and sdept!=计算机,先找到计算机专业中的年龄集合(22,20,17),.,29,以上嵌套查询可以转换为: select sname,sage from student where sage (select max(sage) from student where sdept=计算机) and sdept!=计算机,.,30,例2:查询其他系中比计算机系所有学生年龄 都小的学生姓名和年龄。 select sname,sage from student where sage (select min(grade) from score where cno=3245) order by grade desc,.,34,10、select student.sno,student.sname,grade from score,student where score.sno=student.sno and cno=3105 and grade all (select grade from score where cno=3245) order by grade desc 或者 select student.sno,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论