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<>'语文')

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

相关推荐
风骏时光牛马13 分钟前
大模型底层架构与AI源码深度剖析
前端
掘金挖土1 小时前
前端手摸手跑路之 AI 应用开发(九)
前端·后端
这个DBA有点耶2 小时前
从症状到根因:数据库监控指标的分层方法与静态/动态阈值策略
数据库·mysql·代码规范
中文呀4 小时前
三维 GIS 离线缓存实战:逆向 CesiumOfflineCache,并亲手为它补上"自动更新"能力
前端
ProcessOn4 小时前
纯干货!MySql实战02:一条SQL更新语句执行流程大探索!
sql·mysql·sql server
这个DBA有点耶4 小时前
DELETE没删多少数据,表却越来越大——InnoDB删除空洞与页合并失效的根因
数据库·mysql·代码规范
前进前进前进4 小时前
TRAE 全链路实战:做了一个警示牌一键生成器:纯 Canvas、单文件、零依赖
前端
小万7425 小时前
God‘s Eye View 深度解析:15 路公开信号 + CesiumJS 的浏览器级 OSINT 控制台,60 帧渲染的“真实间谍卫星模拟器”
前端
IT星宿5 小时前
浏览器标签页全是一个图标:我用名字哈希生成 favicon,让每个项目都有身份标识
前端