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

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

相关推荐
默_笙2 分钟前
👍 手写一个 MCP 文件读取服务:原来 AI 能"读"我的文件,全靠这个协议
前端·javascript
饮茶三千2 分钟前
基于 WangEditor 的富文本编辑器定制实践
前端·vue.js·设计模式
kyle~2 分钟前
前端 --- Toast 轻量级弱反馈提示组件
前端
玉宇夕落4 分钟前
Vue3 + 流式输出学习
前端
努力成为AK大王5 分钟前
CSS 入门完整笔记
前端·css
微笑挖矿12 分钟前
大模型流式渲染:为什么"逐字显示"远比你想的复杂
前端
weedsfly13 分钟前
前端开发中的装饰器模式——给函数和组件“加壳”的艺术
前端·javascript·面试
zhedream14 分钟前
那个从没改过的页面,为什么突然坏了?
前端·vue.js
工业HMI实战笔记22 分钟前
HMI性能优化技巧:告别卡顿,打造流畅体验
前端·ui·性能优化·自动化·交互
QN1幻化引擎30 分钟前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法