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

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

相关推荐
前进的李工4 小时前
飞书企业知识库设计+后端高频八股
数据库·redis·后端·mysql·性能优化·飞书·rag
SailingCoder5 小时前
【封闭结束1】端到端交付:Agent 一次请求如何走完全链路
前端·安全·ai·面试·agent·harness
为思念酝酿的痛6 小时前
MySQL数据类型
数据库·mysql
风骏时光牛马7 小时前
AI多模块协同自动化工作流
前端
zhangminghuan7 小时前
前端面试应对技巧
前端·面试·职场和发展
程序员塔塔喵7 小时前
【MySQL篇|第二篇】慢SQL及其优化手段
sql·mysql·慢sql优化
计算机魔术师8 小时前
潜伏14年的PHP致命洞,AI跑两天就交卷了
前端
IT_陈寒8 小时前
Vue的这个响应式陷阱我居然现在才发现
前端·人工智能·后端
为思念酝酿的痛8 小时前
MySQL数据库基础
数据库·mysql