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

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

相关推荐
shmily麻瓜小菜鸡12 分钟前
前端实现图片与PDF文字识别敏感信息打码方案
前端·pdf·状态模式
打工仔折腾 AI18 分钟前
LangChain 工具调用实战:从 Tool 定义到 Agent 执行链路拆解
java·前端·人工智能·后端·python·langchain·ai agent 实战
Dxy123931021621 分钟前
MySQL JSON 生成列索引原理、实战与坑点
mysql·adb·json
wno70427 分钟前
MYSQL 常用合并函数
android·数据库·mysql
汉堡大王952734 分钟前
单次输出拉到100万Token:拆完 Gemini 4 Argon 的续写协议,才看懂谷歌这7个月在憋什么
前端·人工智能·后端
看浪的路人36 分钟前
第9讲:大模型应用监控与可观测性——OpenTelemetry + Prometheus + Grafana 全链路追踪
java·前端·spring
内存过载37 分钟前
用表格模拟甘特图:项目排期的土办法
前端·低代码
葡萄城技术团队1 小时前
用 SpreadJS 数据验证实现省市区三级联动
前端
派小心.1 小时前
A/B实验分层随机种子的稳定性验证
大数据·服务器·前端·数据分析
袋鼠云数栈UED团队1 小时前
从 Skills 到可靠的 Bugfix Agent
前端