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

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

相关推荐
雪芽蓝域zzs30 分钟前
第三十六节:keep-alive 页面缓存(Vue3 + 后台管理系统)
前端·vue.js·缓存
言乐630 分钟前
Python加速器2视频网页加速器
前端·javascript·css·python·音视频
ShineWinsu36 分钟前
对于MySQL:索引的解析
linux·数据库·mysql·面试·笔试·索引·海量数据
IT_陈寒1 小时前
为什么我的Java Stream操作总是默默吃掉异常?
前端·人工智能·后端
光影少年1 小时前
react navite浏览器 Event Loop 和 Node Event Loop 的区别
前端·javascript·react native·react.js·前端框架
90后的晨仔1 小时前
uni-app 安卓自有证书完全指南
前端
嵌入式学习菌10 小时前
Modbus‑RTU 数据类型分析
开发语言·单片机·bug
CharlesYu0111 小时前
前端性能优化的第一性原理,是不断缩短“用户发起意图 → 获得可用结果”之间的时间
前端
平头哥技术团队12 小时前
Day 21 _ 页内锚点_给每段起个 id,目录写 href=_#id_,点一下页面就滚到那一段
前端·html·html5