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

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

相关推荐
Rabi'2 分钟前
编译ATK源码
前端·webpack·node.js
SoaringHeart22 分钟前
Flutter组件封装:视频播放组件全局封装
前端·flutter
神秘面具男0332 分钟前
MySQL 从基础到实践
数据库·mysql
离&染2 小时前
vue.js2.x + elementui2.15.6实现el-select滚动条加载数据
前端·javascript·vue.js·el-select滚动加载
inferno2 小时前
HTML基础(第一部分)
前端·html
kirinlau2 小时前
pinia状态管理在vue3项目中的用法详解
前端·javascript·vue.js
2301_767902642 小时前
MySQL 入门
数据库·mysql
zhuà!2 小时前
腾讯地图TMap标记反显,新增标记
前端·javascript·vue.js
未知原色2 小时前
web worker使用总结(包含多个worker)
前端·javascript·react.js·架构·node.js
ttod_qzstudio2 小时前
CSS改变图片颜色方法介绍
前端·css