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

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

相关推荐
东风破_几秒前
AJAX 异步请求:从回调地狱到 async/await,到底解决了什么?
前端
Larcher1 分钟前
JS 数据类型的八重人格与内存真相
前端·javascript
matrixmind14 分钟前
aiomysql:异步场景下的 MySQL 驱动
android·数据库·mysql·其他
星辰徐哥5 分钟前
工具推荐:HTML5+AI开发必备的前端调试工具
前端·人工智能·html5
zh_xuan5 分钟前
诡异Bug:输入框删除字符,却越删越多
android·bug
Full Stack Developme6 分钟前
Linux Shell 教程概览
linux·前端·chrome
Maimai108086 分钟前
Web3 前端实时通信如何落地:从 SSE 订阅到行情、订单与账户状态更新
前端·javascript·react.js·前端框架·web3·状态模式
星辰徐哥6 分钟前
技能提升:自然语言处理在HTML5前端的应用
前端·自然语言处理·html5
the_answer7 分钟前
React Server Components 深度剖析:前端架构的范式革命
前端
徐小夕8 分钟前
我们放弃了单Agent方案:HiCAD 3.0 用 Harness 做多Agent编排,把3D建模的准确率提升了30%
前端·算法·github