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

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

相关推荐
014-code2 分钟前
Vue 中 data 为什么是函数而不是对象?
前端·javascript·vue.js
V1ncent Chen5 分钟前
从零学SQL 02 MySQL架构介绍
数据库·sql·mysql·架构·数据分析
大母猴啃编程6 分钟前
MySQL内置函数
数据库·sql·mysql·adb
下北沢美食家10 分钟前
HTML面试题
前端·html
巴巴博一13 分钟前
UniApp 纯前端实现企业级购物车:Vuex + Storage 多用户状态管理闭环方案
前端·vue.js·uni-app·状态模式
浮桥16 分钟前
uniapp页面列表列表请求hook记录
前端·javascript·uni-app
bluceli17 分钟前
前端微前端架构实战指南:构建可扩展的大型应用架构
前端
阿懂在掘金18 分钟前
Vue Asyncx 库三周年,回顾起源时的三十行代码
前端·typescript·开源
一只不会编程的猫18 分钟前
Echart 3D环形图
前端·javascript·3d
脸大是真的好~18 分钟前
黑马AI+前端教程 01-HTML-Trae-F12-live Server-标签-块级和内联元素-图片格式-路径
前端·html