数据库sql语句(经典)

news/2024/11/8 15:02:46/

例题:

先来讲讲not in 和not exists的区别,再开始今天的例题(和in,exists相反)

not in内外表做笛卡尔积,然后按照条件查询,没有用到索引

not exists是对外表进行循环,每次循环再对内表进行查询,先查的外表的内容,不是子查询的内容,并且依然用到了表上的索引

两者最大的区别是:not in只能返回一个字段

如:

select week6s.SNO 一门课程没选的学生学号
from week6s
where week6s.SNO not in(
select week6sc.SNO
from week6sc
group by week6sc.SNO)
select week6s.SNO 一门课程都没选修的学生学号
from week6s
where  not exists(
select *
from week6sc
where week6s.SNO=week6sc.SNO
)

not in能表示的not exists也能表示(其实所有带in,比较运算符,any,all的子查询都能用exists表示),并且注意观察,两者引用的方式也不同,返回的结果是:

再着重讲一下两者的引用过程

not in相当于笛卡尔积的运算过程,找到符合not in前的一个字段的条件,不能是name,id not in

exists子查询的返回结果只返回真值或假值,所以给出列明没有实际的意义

对于exists,内层循环结果非空,外层where返回真值

对于not exists,内层循环结果非空,外层where返回假值

select *
from week6sc
where week6s.SNO=week6sc.SNO

内层循环返回结果非空,所以not exists 为false,这里的记录在exists会被记录下来

补充:‘=’和‘in’

当确切知道内层循环返回单值时,可用比较运算符(‘>’'<''=''!=')

select sno,sname,sdept
from student
where(select sdept from student where sname='刘')=sdept

在sql server中这条语句不会报错,但是sql标准要求子查询一定要跟在比较符之后,所以这条语句是错的。

1)检索至少选修一门课程的学生学号
select week6s.SNO 至少选修一门课程的学生学号
from week6s,week6sc
group by week6s.SNO,week6sc.SNO
having week6s.SNO=week6sc.SNO

2)检索一门课程都没选修的学生学号

select week6s.SNO 一门课程没选的学生学号
from week6s
where week6s.SNO not in(
select week6sc.SNO
from week6sc
group by week6sc.SNO)

3)检索至少选修两门课程的学生学号
select week6sc.SNO 至少选修两门课程的学生学号
from week6sc
group by week6sc.SNO
having count(week6sc.SNO)>1
 

 这里也可以检索所有选了课的学生-选了小于两门课的学生,思路相同只是写起来比较繁琐

4)检索恰好选修一门课程的学生学号
select week6sc.SNO 选修了一门课程的学生学号
from week6s,week6sc
group by week6s.SNO,week6sc.SNO
having week6s.SNO=week6sc.SNO
and week6sc.SNO not in(
select week6sc.SNO
from week6sc
group by week6sc.SNO
having count(week6sc.SNO)>1
)

5)检索至少选修三门课程的学生学号

select week6sc.SNO 至少选修三门课程的学生学号
from week6sc
group by week6sc.SNO
having count(week6sc.SNO)>2

6)检索全部课程都选的学生学号(不存在一门课,成绩表中找不到他这门课的成绩记录)
select week6s.SNO 选修了全部课程的学生学号
from week6s
where not exists(select * from week6cwhere not exists(select * from week6scwhere week6c.CNO=week6sc.CNOand week6sc.SNO=week6s.SNO)
)

7)检索选了S2所选全部课程的学生学号(同理,不存在一门课S2选了,而学生x没选)
select distinct sc1.SNO 选了S2所选的全部课程的学生学号
from week6sc sc1
where not exists(select * from week6sc sc2where sc2.SNO='S2'and not exists(select *from week6sc sc3where sc3.SNO=sc1.SNOand sc3.CNO=sc2.CNO)
)

 法2:

select distinct SNO
from week6sc
where CNO>=all(select CNOfrom week6scwhere SNO='s2'
)


http://www.ppmy.cn/news/64070.html

相关文章

使用 ChatGPT 辅助学习——为自己找一个老师

我们每个人都有许多标签&#xff0c;例如高中生、成绩中等、文科&#xff0c;根据这些标签我和其他拥有相同标签的人分配了相同的教程、班级和老师&#xff0c;这可以带来效率上的提升&#xff0c;因为同一份教程、老师就可以服务几十上百人&#xff0c;而无须为每个人定制&…

poll()系统调用

#include <poll.h> int poll(struct pollfd fds[], nfds_t nfds, int timeout);Returns number of readf file descriptors, 0 on timeout, or -1 on error.参数fds列出了我们需要poll()来检查的文件描述符。该参数为pollfd结构体数组&#xff0c;其定义如下&#xff1a;…

【C++】继承和多态、public、private、protected、重写

区分继承与多态、辨别public、protected、private 继承与多态的概念继承与多态的区别与联系区别&#xff1a;联系&#xff1a;示例结果&#xff1a; 继承和访问的权限说明示例&#xff1a;结果 结论 继承与多态的概念 面向对象三大原则&#xff1a;封装、继承、多态。继承是一种…

面向开发人员的 ChatGPT 提示词教程 - ChatGPT Prompt Engineering for Developers

面向开发人员的 ChatGPT 提示词教程 - ChatGPT Prompt Engineering for Developers 1. 指南(原文: Guidelines)1-1. 提示的指南(原文: Guidelines for Prompting)1-2. 配置1-3. 提示语原则(原文: Prompting Principles)原则 1: 写出清晰而具体的指示(原文: Write clear and spe…

【LeetCode】《LeetCode 101》第七章:动态规划

文章目录 7.1 算法解释7.2 基本动态规划&#xff1a;一维70. 爬楼梯&#xff08;简单&#xff09;198.打家劫舍&#xff08;中等&#xff09;413. 等差数列划分&#xff08;中等&#xff09; 7.3 基本动态规划&#xff1a;二维64. 最小路径和&#xff08;中等&#xff09;542. …

多项式指数函数(多项式 exp)

板题&#xff1a;P4726 【模板】多项式指数函数&#xff08;多项式 exp&#xff09; 已知 g ( x ) g(x) g(x)&#xff0c;你要求出 f ( x ) ≡ e g ( x ) ( m o d x n ) f(x)\equiv e^{g(x)} \pmod{x^n} f(x)≡eg(x)(modxn)。 前置知识&#xff1a;多项式求逆&#xff0c;…

C++ MFC调用JS代码获取返回值

C有时候会需要调用JS代码&#xff0c;这对于C来说或者对于国内来说一直是比较蛋疼的问题&#xff0c;主要是资料少&#xff0c;微软提供了一个COM组件&#xff0c;里面包含有JS引擎&#xff0c;这个组件就是&#xff1a;msscript.dll。 此文件在C:\Windows\SysWOW64目录下&…

webpack 5 实战(3)

四十一、代码拆分方式 通过Webpack实现前端项目整体模块化的优势很明显&#xff0c;但是它同样存在一些弊端&#xff0c;那就是项目当中所有的代码最终都会被打包到一起&#xff0c;试想一下&#xff0c;如果说应用非常复杂&#xff0c;模块非常多的话&#xff0c;那打包结果就…