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

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

相关推荐
Setsuna_F_Seiei3 小时前
性能优化 - 列表分页查询的优化
前端·vue.js·性能优化
Setsuna_F_Seiei4 小时前
性能优化 - Vue.js 的懒加载的优化封装
前端·vue.js·性能优化
excel5 小时前
Prisma 查询不到 SQL 更新后的字段?先别急着重新打包
前端
城北徐宫6 小时前
MySQL基础与库、表操作
数据库·mysql
狗头大军之江苏分军7 小时前
《潮水漫过十七岁》倒计时
前端·javascript·后端
小码哥0688 小时前
租房平台源码部署验收:房源样例、区域字典与看房预约逐项验证
前端·javascript·vue.js·租房小程序·租房系统·租房软件
htzyl2068 小时前
JS原型链与继承:class 底下到底是什么
前端·javascript·ecmascript 6
一条Go8 小时前
15 套品牌色 + 双模式:把颜色关进三层 token
前端·github
htzyl2068 小时前
JS-关于闭包捕获与释放
前端·javascript
狗头大军之江苏分军8 小时前
《潮水漫过十七岁》好久没写了
前端·javascript·后端