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

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

相关推荐
风骏时光牛马6 分钟前
AI提示词工程:高效构建精准指令的方法论
前端
葡萄城技术团队15 分钟前
一个好的 BI Dashboard 到底应该怎么设计?
前端
晴天162 小时前
前端 postMessage 使用场景
前端·javascript·网络
zhanghaha13143 小时前
HTML系列教程:15_标签 全称 + 简写 超详细讲解(新手专用)
前端·html
zhanghaha13143 小时前
HTML系列教程:14_HTML 表单与输入框 <form>、<input> 零基础详解
java·前端·javascript
IMPYLH3 小时前
HTML 的 <samp> 元素
前端·网络·html
IMPYLH3 小时前
HTML 的 <script> 元素
前端·html
weixin_493503678 小时前
Vue3 前端生成 PDF:会员证书与活动签到表的三种打印方案与踩坑记录
前端·pdf·状态模式
天衍四九-9 小时前
MySQL中如何定位慢查询?如果SQL语句执行很慢,如何分析和优化?
数据库·sql·mysql
尚久龙10 小时前
mysql-5.7.26-winx64的安装步骤
数据库·mysql