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 分钟前
开源 Claude Code + Codex + 面板 的未来vibecoding平台
前端·后端·github
lyrieek19 分钟前
pgadmin的导出图实现,还在搞先美容后拍照再恢复?
前端
永远是我的最爱24 分钟前
基于.NET的小小便利店前台收银系统
前端·sqlserver·.net·visual studio
从文处安25 分钟前
「九九八十一难」第一难:前端数据mock指南(TS + VUE)
前端
筵陌32 分钟前
MySQL索引及其底层原理(上)
mysql
Zhencode1 小时前
Vue3 响应式依赖收集与更新之effect
前端·vue.js
x-cmd1 小时前
[x-cmd] jsoup 1.22.1 版本发布,引入 re2j 引擎,让 HTML 解析更安全高效
前端·安全·html·x-cmd·jsoup
怣501 小时前
MySQL子查询零基础入门教程:从小白到上手(零基础入门版)
数据库·mysql
天下代码客1 小时前
使用electronc框架调用dll动态链接库流程和避坑
前端·javascript·vue.js·electron·node.js
奶茶精Gaaa1 小时前
测试能力提升--Bug分析能力
bug