sqlserver exists,not exists的用法
发布时间:2024-01-22 02:13:26
学生表:create table student
(
id number(8) primary key,
name varchar2(10),deptment number(8)
)
选课表:create table select_course
(
ID NUMBER(8) primary key,
STUDENT_ID NUMBER(8) foreign key (COURSE_ID) references course(ID),
COURSE_ID NUMBER(8) foreign key (STUDENT_ID) references student(ID)
)
课程表:create table COURSE
(
ID NUMBER(8) not null,
C_NAME VARCHAR2(20),
C_NO VARCHAR2(10)
)
student表的数据:
ID NAME DEPTMENT_ID
---------- --------------- -----------
1 echo 1000
2 spring 2000
3 smith 1000
4 liter 2000
course表的数据:
ID C_NAME C_NO
---------- -------------------- --------
1 数据库 data1
2 数学 month1
3 英语 english1
select_course表的数据:
ID STUDENT_ID COURSE_ID
---------- ---------- ----------
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 3 2
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 not exists
(select * from course c where not exists
(select * from select_course sc where sc.student_id=ts.id and sc.course_id=c.id));
ID NAME DEPTMENT_ID
---------- --------------- -----------
1 echo 1000
2.查询没有选择所有课程的学生,即没有全选的学生。(存在这样的一个学生,他至少有一门课没有选),
分析:只要有一个门没有选,即select * from select_course sc where student_id=t_student.id and course_id
=course.id 有一条为空,即not exists null 为true,此时select * from course有查询结果(id为子查询中的course.id ),
因此select id,name from t_student 将执行查询(id为子查询中t_student.id )。
SQL> select id,name from t_student where exists
(select * from course where not exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
2 spring
3 smith
4 liter
3.查询一门课也没有选的学生。(不存这样的一个学生,他至少选修一门课程),
分析:如果他选修了一门select * from course结果集不为空,not exists 判断结果为false;
select id,name from t_student 不执行查询。
SQL> select id,name from t_student where not exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
4 liter
4.查询至少选修了一门课程的学生。
SQL> select id,name from t_student where exists
(select * from course where exists
(select * from select_course sc where student_id=t_student.id and course_id=course.id));
ID NAME
---------- ---------------
1 echo
2 spring
3 smith


猜你喜欢
- 在这之前,你首先得了解Python中的PIL库。PIL是Python Imaging Library的简称,PIL是一个Python处理图片
- 本文实例为大家分享了H5+css3+js搭建带验证码的登录页面,供大家参考,具体内容如下login.html<!DOCTYPE HTM
- import datetime as dtdef log_time(message, time=None): if time i
- 首先,自学Python是能够找到相关工作的。Python语言在近几年的上升趋势非常明显,语言生态也越来越健全,在Web开发、大数据开发、人工
- Python 中要创建对象列表:声明一个新变量并将其初始化为一个空列表。使用 for 循环迭代范围对象。实例化一个类以在每次迭代时创建一个对
- 在我的前一篇教程《九宫格基本布局》中,我介绍了用相对定位加绝对定位的方法来制作九宫格的基本布局。这是一种比较符合人们惯性思维的方法,好像制作
- python读取npy文件时,太大不能完全显示,其解决方法当用python读取npy文件时,会遇到npy文件太大,用print函数打印时不能
- 0x00 字符的编码计算机毕竟是西方国家的发明,最开始并没有想到会普及到全世界,只用一个字节中的7位(ASCII)来表示字符对于现在庞大的文
- 程序运行前加载1.导包前面加下划线,运行前加载2.把要加载的写在init函数里面路由设置路由的作用:根据不同的请求指定不同的控制器路由函数:
- 翻转一个链表样例:给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null一
- 本文实例讲述了Python开发的实用计算器。分享给大家供大家参考,具体如下:实现功能:图形界面PyQt,输入框,+,—,*,/ ;乘方 ,开
- 运行方法: 1. 打开python2 IDLE; 2. 输入 fro
- 1. 常见命令连接本地数据库与远程数据库(172.16.xx.xx:3306):mysql -h localhost -u root -p1
- Unittestunittest大家应该都不陌生。它作为一款博主在5-6年前最常用的单元测试框架,现在正被pytest,nose慢慢蚕食。渐
- 一个单步的动作,用了这个脚本,就可以重复执行100遍1000遍。上面就是一个路径描边100遍的效果,吼吼~ 不知道大家明白用处没有?(以前老
- centos7之Python3.74安装安装版本:Python3.74系统版本:centos7系统默认安装Python2.7,保留。安装/u
- 最近github上开源了一个"新语言"vlang,火的不得了,我不信,于是乎,尝试了一下,真香。以下内存均来自https
- Frame切换在本文中,将介绍如何使用 Frame tkraise() 方法在 Tkinter 应用程序中的Frame之间切换。1、Fram
- 我就废话不多说了,大家还是直接看代码吧~package main import ("fmt""time&quo
- 最简单的数组合并我们只要使用array_merge即可array_merge()将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数