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

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

相关推荐
Можно几秒前
pages.json 和 manifest.json 有什么作用?uni-app 核心配置文件详解
前端·小程序·uni-app
小尔¥几秒前
MySQL故障排查与优化
运维·数据库·mysql
hzhsec3 分钟前
钓鱼邮件分析与排查
服务器·前端·安全·web安全·钓鱼邮件
道清茗17 分钟前
【MySQL知识点问答题】锁机制、索引优化与数据库恢复方法
数据库·mysql
#做一个清醒的人21 分钟前
Electron 保活方案:用子进程彻底解决原生插件崩溃问题
前端·electron·node.js
四千岁22 分钟前
Obsidian + jsDelivr + PicGo = 免费无限图床:一键上传,全平台粘贴即发
前端·程序员·github
慧一居士23 分钟前
CSS中 ::deep 作用,使用场景,使用方法介绍
前端·css·vue.js
周聪灬26 分钟前
iOS runtime(2)-class结构和消息转发机制
前端
周聪灬28 分钟前
浅谈对Objective-C的对象本质的理解
前端