mysql exists 和not exists 联合使用的bug

sql 复制代码
select * from student a  
where a.age>15
and
 exists(select 1 from score s where s.student_id = a.id and s.name='数学')
and 
 not exists (select 1 from score s where s.student_id=a.id and s.name<>'语文')

以上语句的含义:

查询出年龄大于15岁的并且参加过数学考试 但是没有参加过语文考试的学生。

也许会说这没有问题,但是当score表中有多条数据时,查询的结果就会出现重复的数据。

解决办法:

将not exists 换成 not in

sql 复制代码
select * from student a  
where a.age>15
and
 exists(select 1 from score s where s.student_id = a.id and s.name='数学')
and 
 a.id not in (select s.student_id from score s where  s.name<>'语文')

为什么查询结果会出现重复的数据,具体原因未知。

相关推荐
Hilaku4 小时前
一个前端工程师除了写代码,还能靠什么赚钱?
前端·javascript·程序员
程序员Sunday4 小时前
JavaScript 闭包是什么?为什么变量没有被释放,什么时候会内存泄漏?
前端·javascript
JarvanMo4 小时前
Flutter 终于有自己的 React Flow 了,vyuh_node_flow 上手实录
前端
狂师4 小时前
爆火!新一代e2e AI测试框架,单日狂揽1500 star
前端·人工智能·测试
啵啵啵鱼5 小时前
对数据的增删改查
数据库·mysql·ai·idea·ai编程·navicat·obsidian
靠沿5 小时前
MySQL数据库基础——索引的使用&alter、explain关键字
java·数据库·mysql
Mortalbreeze5 小时前
MySQL 基础篇(八):复合查询之多表查询、内外连接、子查询
数据库·mysql
恋猫de小郭5 小时前
Flitz ,一个把 Flutter 玩成热插拔 Preview 的有趣项目,不错的思路
android·前端·flutter
Xxtaoaooo5 小时前
NocoDB在线表格数据库:fnOS部署、MySQL可视化与远程协作实操
数据库·mysql·docker·cpolar·nocodb
闭包不眠5 小时前
Word转HTML后体积不对,裁掉的截图原图还在
前端·图像处理·人工智能·计算机视觉·html·word