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

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

相关推荐
我不吃饼干8 小时前
TypeScript 类型体操练习笔记(二)
前端·typescript
光影少年8 小时前
浏览器渲染原理?
前端·javascript·前端框架
小白探索世界欧耶!~9 小时前
Vue2项目引入sortablejs实现表格行拖曳排序
前端·javascript·vue.js·经验分享·elementui·html·echarts
GISer_Jing10 小时前
前端营销(AIGC II)
前端·react.js·aigc
t***442311 小时前
MySQL 导出数据
数据库·mysql·adb
NEXT0611 小时前
深度解析 JWT:从 RFC 原理到 NestJS 实战与架构权衡
前端·typescript·nestjs
翔云12345611 小时前
MySQL主从库复制中,主库如何查找对应日志文件位置
数据库·mysql
程序员林北北12 小时前
【前端进阶之旅】节流与防抖:前端性能优化的“安全带”与“稳定器”
前端·javascript·vue.js·react.js·typescript
寻星探路12 小时前
【前端基础】HTML + CSS + JavaScript 快速入门(三):JS 与 jQuery 实战
java·前端·javascript·css·c++·ai·html
Mr_star_galaxy12 小时前
【MySQL基础】视图和权限管理
数据库·mysql