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

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

相关推荐
前端snow21 分钟前
ai agent --- redis 缓存
前端
duanshu_1 小时前
官网表单与下载异常怎么排查?五个环节定位故障
前端·自动化·flyweight
Zhou1411361 小时前
SpringBoot_03_Web开发
前端·spring boot·python
辻弋2011 小时前
【无标题】
大数据·服务器·前端·搜索引擎·开源软件
夜雪一千2 小时前
MySQL外键彻底入门:是什么、怎么用、什么时候不该用
数据库·mysql
FluxArt2 小时前
GPT Image 2.5 中文提示词教程:Flux Art 可复用模板
前端·gpt·算法
搬砖的小码农_Sky3 小时前
AI Agent:Windows上Node.js安装后 npm -v报错
前端·npm·node.js·人机交互
FYKJ_20103 小时前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
计算机魔术师3 小时前
半年翻倍!OpenAI再融300亿,估值冲到1.4万亿
前端
研☆香4 小时前
jQuery + Swiper.js 实现动态渲染轮播图
前端·javascript·jquery