第7讲-SQL语言复杂查询--(NOT)Exists
2016-04-29 11:20
615 查看
SQL语言复杂查询-(NOT) Exists
语法:[not] Exists(子查询)
语义:子查询有无元组存在
例:检索选修了赵三老师主讲课程的所有同学姓名。
上面可以直接写为:
上面说明,不加not的Exists语句完全可以不使用的。然而not Exists却可以实现很多功能
例:检索学过001号老师主讲的所有课程所有同学的姓名
例:列出学过李明老师任何一门课程的所有同学姓名
语法:[not] Exists(子查询)
语义:子查询有无元组存在
例:检索选修了赵三老师主讲课程的所有同学姓名。
select DISTINCT Sname From student where exists (select * From SC,Course,Teacher where SC.C#=Course.C# and SC.S#=student.S# and course.T#= Teacher.T# and Tname='赵三');
上面可以直接写为:
select DISTINCT Sname From student, SC,Course,Teacher where SC.C#=Course.C# and SC.S#=student.S# and course.T#= Teacher.T# and Tname='赵三');
上面说明,不加not的Exists语句完全可以不使用的。然而not Exists却可以实现很多功能
例:检索学过001号老师主讲的所有课程所有同学的姓名
select sname From student where not Exists --不存在 (select * From Course --有一门001老师主讲的课程 where Course.T#='001' and not Exists --没学过 (select * From SC where S#=student.S# and C#=Course.C#)); --语义:不存在有一门001号老师讲授的课程·没学过
例:列出学过李明老师任何一门课程的所有同学姓名
select Sname From student where not Exists --不存在 ( select * From Course,Teacher SC --学过一门李明老师的课程 where Tname='李明' and Course.T#=Teachesr.T# and Course.C#=SC.C# and S#=student.S#; --语义:不存在学过一门李明老师的课程
相关文章推荐
- SQL Server 批量插入数据的两种方法
- redis 基本命令学习一(strings、hashes、lists)
- SQL架构信息读取
- 批量修改数据库中字段的数据类型
- mysql常用操作
- 大并发大数量中的MYSQL瓶颈与NOSQL介绍
- oracle存储过程模板
- mysql-5.7.12-winx64安装版配置、使用
- SQL查询优化技巧
- 根据当前位置和数据库经纬度排序
- WCF SqlParameter序列化问题解决方案
- MySql外键学习总结
- Node.js和MongoDB——从MongoJS开始
- redis安装过程
- mybatis sql 映射文件问题
- mybatis sql 的一些问题 with。。。as。。。
- mogoTemplate 模板 实现mongodb java的操作 ,根据经纬度查询周边
- 数据库
- mongodb lbs 坐标定位 java操作版本
- SQLserver2008如何把表格变量传递到存储过程中