当前位置:网站首页>数据库超话(四)
数据库超话(四)
2022-07-27 15:28:00 【泡泡里的月亮】
今天遇到一条SQL语句,久久没能理解,在网上搜索大量资料,才弄懂了这条语句的含义,在此记录一下。
select SN from S
where (not exists (select * form C
where not exists (select * from SC
where Sno = S.Sno and Cno = C.Cno)
我们看到这是含有not exists 语句的相关子查询。解释一:
语句的执行顺序为类似为两层for循环,第一层for循环为S表的记录,第二层for循环为C表中的记录。下面我们具体分析。
当外层S->S1 ,
进入内层C->C1
1)判断C1是否有人选的课,若C1没人选,保留C1,有人选,去掉C1,
C->C2
2)判断C2是否有人选的课,若C2没人选,保留C2,有人选,去掉C2,
如此反复,留下的是没有人选择的课程
遍历完C表,进入外层判断
3)判断这个学生选的课中有没有C1,若没有C1,保留S1,有C1,去掉S1
S-S2
如此反复
那么,选出了选修所有课的学生
第一个解释2我认为逻辑山存在问题
解释2:
exists和 not exists 谓词,首先是从父表取出记录,去匹配where后面的条件,这一点和in不同,in是先从子表匹配出的记录作为父表的查询依据。
那么,分析这句话,
select SN from S 选出一个学生
where (not exists 他不存在 不存在什么呢
select * form C 不存在一门课
where not exists 这门课不存在 不存在什么呢
select * from SC where Sno = S.Sno and Cno = C.Cno 它在选课表里不存在
也就是说,这句话的意思是:
选出了学生 ,这些学生不存在选课表里不存在的课
即选出了选修所有课的学生
边栏推荐
- Following the example of IE, is the decline of Firefox inevitable?
- 立创EDA——原理图的布局与检查(三)
- App crash collection and analysis
- day07 作业
- Select structure
- 苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条...
- 下棋机器人折断7岁男孩手指,网友:违反了机器人第一定律
- Chen Yili of ICT Institute: reducing cost and increasing efficiency is the greatest value of cloud native applications
- MySQL - linked table query
- MySQL: 函数
猜你喜欢

The 7-year-old boy broke his finger by AI robot just because he played chess too fast?

Gradient ring progress bar

三表联查1

诸神黄昏,“猫抖快”告别大V时代

立创EDA——原理图的布局与检查(三)

Smart fish tank design based on stm32

How to extract tables from PDF through C /vb.net

记一次 .NET 某智慧物流 WCS系统 CPU 爆高分析

Kubernetes Chapter 8: deploy NFS system with kubernetes to complete database persistence (kubernetes work practice class)

KMP template - string matching
随机推荐
移动端页面布局
URL return nil and urlhash processing
Hidden iframe design details of SAP ui5 fileuploader
一文理解分布式开发中的服务治理
Sharing of local file upload technology of SAP ui5 fileuploader
Microsoft silently donated $10000 to curl, which was not notified until half a year later
CUE语言基础入门:CUE是一门为配置而生的语言
App crash collection and analysis
MySQL: 函数
交换机和路由器技术-02-以太网交换机工作原理
Maximum number less than n
Basic use and optimization of uitableview
SVM+Surf+K-means花朵分类(Matlab)
UITableView 基本使用和优化
Swift QQ authorized login pit set
Windows与网络基础-15-本地安全策略
js中的函数
Flex弹性盒布局
Shell programming specifications and variables
meta-data 占位符的引用