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

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

相关推荐
Web打印1 小时前
Phpask(php集成环境)之15 phpstudy转移到phpask
数据库·mysql
岱宗夫up8 小时前
FastAPI入门(上篇):快速构建高性能Python Web API
开发语言·前端·python·fastapi
紫陌涵光8 小时前
112. 路径总和
java·前端·算法
漠月瑾-西安9 小时前
CVE-2025-55182漏洞解析:你的React项目安全吗?
前端·安全·react.js
No丶slovenly9 小时前
flutter笔记-输入框
前端·笔记·flutter
国产化创客9 小时前
ESP32+Web实现智能气象站
前端·物联网·智能家居·智能硬件
coderYYY10 小时前
VSCode终端启动报错
前端·ide·vscode·npm·编辑器
tod11311 小时前
Redis 数据类型与 C++ 客户端实践指南(redis-plus-plus)
前端·c++·redis·bootstrap·html
Sylvia33.11 小时前
火星数据:棒球数据API
java·前端·人工智能
weixin1997010801611 小时前
1688商品详情页前端性能优化实战
前端·性能优化