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

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

相关推荐
codezzzsleep9 分钟前
让个人智能体安全地接触外部资料
java·前端·安全
掘金安东尼29 分钟前
文档不一致,会伤害 AI 编码智能体吗?智能体
前端
xurileidian8 小时前
第 24 篇 · 边玩边学Python · while 与死循环怎么退
服务器·前端·python
zxcvb15310 小时前
运维实践里把备份mysql数据库做稳的一些思路
运维·数据库·mysql
Highcharts.js10 小时前
Highcharts软件绘制图表:用数据构建可视化图形
前端·学习·信息可视化·数据分析·js·开发实战·highcharts
Hilaku11 小时前
2027 制药行业 ERP 系统架构选型笔记:从 GMP 合规追溯到动态 MCP 接入
前端·javascript·程序员
一级新生12 小时前
Dify 迭代节点:从输入输出到九步实操
数据库·人工智能·mysql·低代码·工作流·dify
掘金安东尼13 小时前
人工智能AI
前端
kyriewen13 小时前
AI 面试官筛前端简历,先出局的不是写代码慢的
前端·面试·程序员
BigTopOne13 小时前
10 引用与借用:& 和 &mut
前端