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

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

相关推荐
天蓝色的鱼鱼14 分钟前
React Router v8 来了:react-router-dom 没了,老项目该怎么迁移?
前端·react.js
闪闪发光得欧1 小时前
前端提效新思路:Gemini 3.5 自动化定位 CSS 异常
前端·css
yingyima1 小时前
掌握正则表达式的核心:贪婪与非贪婪匹配的底层机制
前端
奇奇怪怪的1 小时前
文档摄入与 Chunking 策略全对决
前端
Jim6002 小时前
【吃透 MySQL InnoDB连载】第 1 章・解密线上数据库高频故障
mysql
阳火锅2 小时前
😭测试小姐姐终于不骂我了!这个提BUG神器太香了...
前端·javascript·面试
道友可好3 小时前
AI 是最好的混乱放大器:代码熵管理实战
前端·人工智能·后端
猩猩程序员3 小时前
前端学习 AI Agent 开发
前端
Younglina4 小时前
打了3年羽毛球球才发现:我对自己的装备和胜率一无所知
前端·后端
风骏时光牛马4 小时前
Bash脚本高阶实战与常见报错完整代码案例详解
前端