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

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

相关推荐
Dxy12393102166 小时前
MySQL索引完整教程:创建、查看、修改、删除与日常管理
前端·javascript·mysql
剪刀石头布啊6 小时前
js能被遍历的集合有哪些特征,为何能被遍历
前端
剪刀石头布啊6 小时前
js解构
前端
剪刀石头布啊6 小时前
防抖功能的逐步递进
前端
剪刀石头布啊6 小时前
对象的大小比较与面向对象思考
前端
至乐活着8 小时前
MySQL索引底层原理与查询优化实战:从B+树到执行计划
mysql·b+树·查询优化·sql性能·索引原理
最爱老式锅包肉8 小时前
《HarmonyOS技术精讲-ArkWeb》桥接两岸:JSBridge原生与Web互调
前端·华为·harmonyos
IT_陈寒9 小时前
闭包陷阱让我加了两天班,JavaScript你真行
前端·人工智能·后端
踩着两条虫9 小时前
可视化 vs 终端 vs 云端:VTJ.PRO、Claude Code、Codex 三强横评
前端·vue.js·人工智能·低代码·架构
kyriewen9 小时前
Godot 全面封杀 Vibe Coding,作为每天用 AI 写代码的人,我反而觉得这是件好事
前端·ai编程·claude