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

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

相关推荐
酉鬼女又兒25 分钟前
SQL24 统计每个用户的平均刷题数
数据库·sql·mysql
毕设源码-郭学长1 小时前
【开题答辩全过程】以 基于Web的高校课程目标达成度系统设计与实现为例,包含答辩的问题和答案
前端
wuhen_n1 小时前
高阶函数与泛型函数的类型体操
前端·javascript·typescript
一只自律的鸡1 小时前
【MySQL】第六章 子查询
数据库·mysql
ヤ鬧鬧o.2 小时前
多彩背景切换演示
前端·css·html·html5
lethelyh2 小时前
Vue day1
前端·javascript·vue.js
酉鬼女又兒3 小时前
SQL113+114 更新记录(一)(二)+更新数据知识总结
java·服务器·前端
无风听海3 小时前
AngularJS中 then catch finally 的语义、执行规则与推荐写法
前端·javascript·angular.js
weixin_462446233 小时前
一键安装 MySQL 5.7(CentOS 7)自动化脚本详解
mysql·centos·自动化
利刃大大3 小时前
【Vue】组件化 && 组件的注册 && App.vue
前端·javascript·vue.js