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

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

相关推荐
Bug.ink13 分钟前
BUUCTF——WEB(4)
前端·网络安全·靶场·ctf·buuctf
L Jiawen14 分钟前
【Web】RESTful风格
前端·后端·restful
momo(激进版)20 分钟前
前端打包时自动更新版本号
前端
胖虎125 分钟前
UIKit实现一个渐变文字的UILabel(核心思想及实现过程)
前端·mask·渐变文字·ios渐变文字·渐变label
alphardex33 分钟前
一个普通魔法师的 2025 年度总结
前端·年终总结
德莱厄斯1 小时前
AI 纪元 3 年,2025 论前端程序员自救
前端·ai编程·vibecoding
WX-bisheyuange1 小时前
基于Spring Boot的社团管理系统的设计与实现
前端·javascript·vue.js·毕业设计
橙某人1 小时前
LogicFlow 插件魔改实录:手把手教你重写动态分组(DynamicGroup)🛠️
前端·javascript·vue.js
阿蔹1 小时前
UI测试自动化-Web-Python-Selenium-2-元素操作、浏览器操作
前端·python·selenium·ui·自动化
谎言西西里2 小时前
React hooks 之 一篇文章掌握 useState 和 useEffect 的核心机制
前端·react.js