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

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

相关推荐
CIb0la17 小时前
Complete Bug Bounty tool List for free
linux·运维·bug
南游17 小时前
数组判断?我早不用instanceof了,现在一行代码搞定!
前端·javascript
mouseliu17 小时前
pnpm approve-builds报错
前端
JIseven17 小时前
app页面-锚点滚动 和 滚动自动激活菜单
前端·javascript·html
hanyi_qwe18 小时前
Mysql备份与还原
数据库·mysql
AAA阿giao18 小时前
在你的网页中嵌入 Coze 智能客服:一步步打造专属 AI Agent
前端·javascript·人工智能
AAA阿giao18 小时前
深入解析 OOP 考题之 EditInPlace 类:从零开始掌握面向对象编程实战
前端·javascript·dom
时718 小时前
利用requestIdleCallback优化Dom的更新性能
前端·性能优化·typescript
西西学代码18 小时前
flutter---进度条(2)
前端·javascript·flutter
w***153118 小时前
【MySQL数据库】Ubuntu下的mysql
数据库·mysql·ubuntu