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

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

相关推荐
卡兰芙的微笑16 分钟前
get_property --Cmakelist之中
前端·数据库·编辑器
覆水难收呀19 分钟前
三、(JS)JS中常见的表单事件
开发语言·前端·javascript
阿华的代码王国22 分钟前
【JavaEE】多线程编程引入——认识Thread类
java·开发语言·数据结构·mysql·java-ee
猿来如此呀26 分钟前
运行npm install 时,卡在sill idealTree buildDeps没有反应
前端·npm·node.js
hw_happy32 分钟前
解决 npm ERR! node-sass 和 gyp ERR! node-gyp 报错问题
前端·npm·sass
FHKHH36 分钟前
计算机网络第二章:作业 1: Web 服务器
服务器·前端·计算机网络
qq_353233538943 分钟前
【原创】java+springboot+mysql高校社团网系统设计与实现
java·spring boot·mysql
视觉小鸟1 小时前
【JVM安装MinIO】
前端·jvm·chrome
布洛芬颗粒1 小时前
JAVA基础面试题(第二十二篇)MYSQL---锁、分库分表!
java·开发语言·mysql
二川bro2 小时前
【已解决】Uncaught RangeError: Maximum depth reached
前端