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

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

相关推荐
whn19771 小时前
dm9安装初体验
前端
阿黎梨梨1 小时前
从零搞懂 JWT 登录鉴权:一张“电子通行证”的诞生记
前端
用户921080262861 小时前
ChatMessageList 消息列表组件:基于 BubbleList 搭建 AI 对话展示区
前端
HjhIron1 小时前
实战 React + JWT + Zustand:从零搭建登录鉴权系统
前端
晴天161 小时前
DeepSeek Harness 全景技术解析-Day23
前端·deepseek
风月说与山鬼2 小时前
三、uni-app页面配置(pages.json)
前端·uni-app
daols882 小时前
vue 实现基于 vxe-table 构建多维度产品对比表
前端·javascript·vue.js
前端 贾公子2 小时前
第08章:中间件(5)
服务器·前端·javascript
tech_zjf2 小时前
当 AI 把 Next.js Route 越写越快:我为什么做了 next-route-kit
前端·后端
常宇佳2 小时前
vue3 @代指src路径设置
前端·typescript·vue