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

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

相关推荐
程序员黑豆7 小时前
Java字符串详解
java·前端·ai编程
无我Code7 小时前
开发者-2026年中总结
前端·面试·程序员
浮生望9 小时前
React 受控与非受控组件:从表单状态管理到实时校验的完整实践
前端
糖墨夕10 小时前
第二章:AI Agent 到底是个啥?—— 用生活场景把概念讲明白
前端
Csvn10 小时前
⚡ content-visibility: auto —— 长页面渲染提速的隐藏神器,白捡的性能优化
前端
用户9385156350710 小时前
深入理解 Next.js:从 SPA 的 SEO 问题到约定式路由与 SSR 原理
前端·后端·全栈
snow@li10 小时前
web:前端500知识点/速查手册
前端
灯澜忆梦10 小时前
【MySQL12】进阶篇 | SQL优化
数据库·sql·mysql·性能优化
kyriewen12 小时前
今年裁了16万技术人——但字节前端岗反而涨了23%
前端·javascript·面试
用户0595401744612 小时前
把 AI Agent 记忆存储验证从 40 分钟手测压到 3 分钟,pytest + Docker 这套组合太顶了
前端·css