sqlserver exists,not exists的用法
发布时间:2020-09-25 11:55:05 所属栏目:MsSql 来源:互联网
导读:exists,not exists的使用方法示例,需要的朋友可以参考下。
学生表:create table student 选课表:create table select_course 课程表:create table COURSE student表的数据: course表的数据: select_course表的数据: 1.查询选修了所有课程的学生id、name:(即这一个学生没有一门课程他没有选的。) 分析:如果有一门课没有选,则此时(1)select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id存在null, 这说明(2)select * from course c 的查询结果中确实有记录不存在(1查询中),查询结果返回没有选的课程, 此时select * from t_student ts 后的not exists 判断结果为false,不执行查询。 SQL> select * from t_student ts where ID NAME DEPTMENT_ID 2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选), 分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id 因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。 SQL> select id,name from t_student where (select * from course where (select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME 3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程), 分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false; select id,name from t_student 不执行查询。 SQL> select id,0)">not exists (select * from course where (select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME 4.查询至少选修了一门课程的学生。 (select * from select_course sc where student_id=t_student.id and course_id=course.id)); ID NAME (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- sql-server – 在SQL Server 2008 R2计算机上安装IIS的含义
- 如何以编程方式取消SQL Server执行过程
- 寻找一个SQL测试集生成器,理想情况下开源
- 利用ROW_NUMBER() OVER函数给SQL数据库中每一条记录分配行号
- sql-server – SQL Server PDF全文搜索无法在FileStream PD
- sql-server – 什么是LINQ相当于SQL的“IN”关键字
- sql – “重复任务”的设计选项
- LINQ to SQL EntitySet中的漏洞抽象
- 如何查询SQL Server 2000数据库中的数据库角色列表?
- sql – regex_substr中的connect by子句
