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

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

相关推荐
wuk9984 小时前
实现ROS系统的Websocket传输,向Web应用推送sensor_msgs::Image数据
前端·websocket·网络协议
~~李木子~~5 小时前
MySQL 迁移总结报告
数据库·mysql
合作小小程序员小小店5 小时前
web网页开发,在线%考试管理%系统,基于Idea,vscode,html,css,vue,java,maven,springboot,mysql
java·前端·系统架构·vue·intellij-idea·springboot
天天进步20156 小时前
CSS Grid与Flexbox:2025年响应式布局终极指南
前端·css
CodeLongBear6 小时前
MySQL索引篇 -- 从数据页的角度看B+树
mysql·面试
桦06 小时前
MySQL【函数】
数据库·mysql
Boop_wu7 小时前
[Java EE] 计算机基础
java·服务器·前端
Novlan17 小时前
TDesign UniApp 组件库来了
前端
用户47949283569157 小时前
React DevTools 组件名乱码?揭秘从开发到生产的代码变形记
前端·react.js
_Minato_8 小时前
数据库知识整理——SQL数据定义
数据库·sql·mysql·oracle·database·数据库开发·数据库架构