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

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

相关推荐
李少兄9 小时前
MySQL分页重复问题深度剖析
android·数据库·mysql
Komorebi_99999 小时前
LangChain Day5 课程:Agent 智能代理
前端·langchain·大模型
鬼才血脉9 小时前
Idea使用Tomact运行项目,控制台输出中文乱码
tomcat·bug
Yupureki9 小时前
《MySQL数据库基础》8.复合查询
linux·运维·服务器·网络·数据库·mysql
七牛云行业应用9 小时前
别等了!xAI 的终端编码 Agent 来了——Grok Build 0.1 安装到并行 Agent全流程
前端
Asmewill9 小时前
LangGraph学习笔记三(State)
前端
史前码农JH9 小时前
nodejs项目Monorepo模式的基础操作
前端
RD_daoyi9 小时前
Google 网站收录全流程解析:抓取、索引与排名机制详解
前端·javascript·人工智能·学习·搜索引擎·html
@大迁世界9 小时前
AI还替不了的JS能力
开发语言·前端·javascript·人工智能·ecmascript
相忘于江湖4265439 小时前
vs code 代码保存自动格式化
前端·vue