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

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

相关推荐
+VX:Fegn08953 小时前
计算机毕业设计|基于springboot + vue蛋糕店管理系统(源码+数据库+文档)
前端·数据库·vue.js·spring boot·课程设计
Nicander5 小时前
我给 Excalidraw 做了一个本地工作区:DrawSpace
前端·开源
linux_cfan6 小时前
17 · 引擎适配器全景:HLS/DASH/Vimeo/Mux/Cast
前端·javascript·音视频
计算机魔术师7 小时前
烧掉2780亿美元还不够?OpenAI的资本豪赌让人头皮发麻
前端
IT_陈寒8 小时前
Java线程池用错参数,我的服务居然悄悄崩溃了
前端·人工智能·后端
卡布鲁8 小时前
React useMemo 与 useCallback 完全指南:原理、场景与避坑
前端·react.js
百万蹄蹄向前冲8 小时前
密码都对Node.js却连不上Linux数据库
linux·mysql
碳基修炼8 小时前
排查记:本地 devServer 是 http,浏览器却把 302 重定向升级成了 https
前端·http
华章酱8 小时前
MySQL幻读是怎么出现的
数据库·mysql·幻读
步行cgn8 小时前
Spring p 命名空间注入详解
java·前端·spring