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

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

相关推荐
2601_9584925542 分钟前
Optimizing Engagement with Freehead Skate - HTML5 Game - Construct 3
前端·html·html5
茉莉玫瑰花茶1 小时前
工作流的常见模式 [ 1 ]
java·服务器·前端
zhangxingchao2 小时前
AI应用开发六:企业知识库
前端·人工智能·后端
长谷深风1112 小时前
索引提速秘籍【个人八股】
mysql·b+树·索引·索引设计原则·存储引擎优化·索引维护成本·字段选择策略
山峰哥2 小时前
SQL慢查询调优实战:从全表扫描到索引覆盖的完整复盘
前端·数据库·sql·性能优化
红尘散仙3 小时前
一个 `#[uniffi::export]`,把 Rust 接进 React Native
前端·后端·rust
moshuying3 小时前
AI Coding 最大的 token 黑洞,可能根本不是 prompt
前端
Irene19913 小时前
在 WSL 中下载安装 MySQL,连接到 SQLyog(MySQL 安装在 WSL vs Windows 本地对比)
mysql·wsl
红尘散仙3 小时前
一行 `#[specta::specta]`,让 Tauri IPC 有类型
前端·后端·rust