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

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

相关推荐
Jinuss1 天前
源码分析之React中updateContainerImpl方法更新容器
前端·react.js·前端框架
Mr Xu_1 天前
Vue + Element Plus 实现前端导出 Excel 功能详解
前端·javascript·vue.js
仰泳之鹅1 天前
【杂谈】使用Edge浏览器下载文件显示“Microsoft Defender SmartScreen 已阻止此不安全文件”的解决方法
前端·edge
·云扬·1 天前
深入理解MySQL事务:ACID特性、隔离级别与MVCC原理
数据库·mysql·oracle
万邦科技Lafite1 天前
小红书评论数据一键获取,item_reviewAPI接口讲解
大数据·前端·数据库·chrome·电商开放平台
scugxl1 天前
mysql federatedengine 使用
mysql
meng半颗糖1 天前
vue3+tpescript 点击按钮跳转新页面直接通过链接预览word
前端·vue.js·word
麦麦在写代码1 天前
Mysql--DDL语句
数据库·mysql
击败不可能1 天前
vue做任务工具方法的实现
前端·javascript·vue.js
觉醒大王1 天前
医学好投的普刊分享
前端·论文阅读·深度学习·学习·自然语言处理·学习方法