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

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

相关推荐
ZC跨境爬虫3 分钟前
跟着 MDN 学 HTML day_17:媒体与 Web Audio API 自动播放指南——策略、检测与最佳实践
前端·笔记·ui·html·音视频·媒体
技术人生黄勇3 分钟前
GitNexus 把代码库变成知识图谱|审核 AI 产出更清晰,改 Bug 更精准
人工智能·bug
canonical_entropy8 分钟前
Nop Chaos Flux:百度AMIS之后的下一代低代码渲染引擎
前端·低代码·ai编程
时光足迹27 分钟前
Tiptap 简单编辑器模版
前端·javascript·react.js
JSLove40 分钟前
nginx入门
前端·nginx
时光足迹41 分钟前
ThreeJS之GUI控制器
前端·javascript·three.js
时光足迹42 分钟前
Tiptap编辑器
前端·javascript·react.js
时光足迹1 小时前
电子书阅读器之笔记高亮(跨段处理)
前端·javascript·react.js
Dabei1 小时前
Android 副屏(Virtual Display)创建与悬浮窗画中画显示实战
前端·架构