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

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

相关推荐
cui_ruicheng8 小时前
MySQL(四):数据类型与字段设计
数据库·mysql
原则猫10 小时前
HOOKS 背后机制
前端
码语智行10 小时前
首页导航跳转功能深度解析-系统内和系统外
前端
DIY源码阁11 小时前
JavaSwing航班订票管理系统 - MySQL版
数据库·mysql
阿猫的故乡11 小时前
Vue过渡动画从入门到装X:淡入淡出、滑动、列表动画、第三方库全搞定
前端·javascript·vue.js
IManiy11 小时前
总结之Vibe Coding前端骨架
前端
JS菌11 小时前
AI Agent 沙箱双层防护体系:从权限过滤到内核隔离的完整实现
前端·人工智能·后端
Aphasia31112 小时前
从输入URL到页面展示全流程
前端·面试
我叫黑大帅12 小时前
前端如何竖屏固定视口背景
前端·javascript·面试
abcy07121312 小时前
python pandas csv异步后台清洗前端优先返回成功信息
前端·python·pandas