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

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

相关推荐
90后的晨仔37 分钟前
uni-app 跨端布局与适配技术指南
前端
2501_937860942 小时前
MySQL表的操作:创建、查看、修改、删除完整实战
android·数据库·mysql
奇特認6 小时前
mysql 集群技术 3.高可用之 MHA
数据库·mysql
紫禁玄科9 小时前
Shai-Hulud:npm生态的自我复制蠕虫风暴
前端·npm·node.js
东风破_10 小时前
后端API没写好,前端难道干等着吗?
前端
百变梦仔10 小时前
从读项目到纠偏交付:我把 Codex 前端任务闭环升级成了第二版
前端
用户9385156350710 小时前
从前后端分离到前端接口工程:React + MockJS + Vite 解析
前端·后端·全栈
用户9385156350710 小时前
从零在浏览器里跑 DeepSeek-R1:WebGPU + Transformers.js 全链路实战(三)
前端·react.js·typescript
excel10 小时前
当前端行情变差,我们为什么还要坚持?
前端
鸿是江边鸟,曾是心上人11 小时前
快速搭建HTTPS本地开发环境
前端