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

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

相关推荐
纯爱掌门人8 分钟前
聊聊 HarmonyOS 上的应用内通知授权弹窗
前端·harmonyos·arkts
Cdlblbq11 分钟前
搜索会员中心 创作中心Vue2项目一键打包成桌面应用
前端·javascript·vue.js·electron
eason_fan33 分钟前
前端避坑指南:一文吃透 npm 幽灵依赖(Phantom Dependency)
前端·前端工程化
淘矿人33 分钟前
Claude辅助算法设计与优化
人工智能·python·算法·microsoft·github·bug·pygame
前端小万33 分钟前
2026年3月面20个前端
前端
葡萄城技术团队1 小时前
智慧表格(SpreadJS + AI):拥抱 Web 端对话式办公新时代
前端·人工智能
OpenTiny社区1 小时前
电商系统集成GenUI SDK实操指南
前端·开源·ai编程
A_nanda1 小时前
vue实现后端传输逐帧图像数据
前端·javascript·vue.js
YGY顾n凡1 小时前
我开源了一个项目:一句话创造一个AI世界!
前端·后端·aigc