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

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

相关推荐
静默回滚几秒前
逐帧处理视频的内存去哪了,我从看错读数查到没排队
前端
重庆穿山甲16 分钟前
我们不做「看起来很聪明」的知识库:一套企业级 RAG 平台的 37 屏真机实录
前端
风花一世月24 分钟前
🛩️ 我把一段夕阳飞行做成了能按键驾驶的网页(24 秒四段航线,在线直接玩)
前端
用户114818678948431 分钟前
实现一个「配置驱动」的动态表格组件(Vue + Element UI)
前端
iCxhust32 分钟前
51单片机电力载波应用开发(九)51产品化
c语言·数据库·嵌入式硬件·mysql·51单片机·软件工程·c51
数据掘金34 分钟前
小程序分享参数在页面栈中的传递
前端
乐享qicheng34 分钟前
1,自定义表单通俗理解-PC设计APP渲染
前端
为你学会写情书35 分钟前
大屏适配别再只会 transform: scale 了,这套方案更稳
前端
孤独的神36 分钟前
《AI 成了我的工位搭子,也成了我的焦虑来源》
前端
行者全栈架构师40 分钟前
给开源 Rust 工具加一个新检查项,TRAE 帮我从想法跑到构建产物:全链路实战
前端·算法·架构