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

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

相关推荐
程序员老赵4 分钟前
Docker 部署青龙面板:轻松搭建定时任务管理平台
前端·docker·开源
Swot5 分钟前
Asciidoctor 公式数字下标错位问题:原因分析与解决方案
前端
Swot6 分钟前
leo-editor 中将 Markdown 内容转成 asciidoc 格式
前端
狒狒AI18 分钟前
Remotion / GSAP+Three / HyperFrames / Skia 四栈实测:代码渲染知识视频的选栈方法(附开源技能)
前端·人工智能·aigc
修炼的dance19 分钟前
浏览器里导出H.265,8.5秒和55.5秒差在硬件
前端
补码缩写20 分钟前
WebCodecs选编码器,探测之后还要验解码和播放
前端
大文说跨境21 分钟前
亚马逊多账号管理浏览器怎么选?云登/紫鸟/AdsPower 对比
java·前端·自动化
hunter_07623 分钟前
iOS 首页复杂嵌套滚动技术方案:头部固定 + 网格滚动 + Tab 吸顶 + 每 Tab 独立下拉刷新
前端
CoderYanger36 分钟前
Java EE 进阶:4.Spring Web MVC 入门(第一弹)
java·开发语言·前端·spring·面试·java-ee·mvc
用户27886072856737 分钟前
fallback 会盖掉平台提示词:system、空数组和 systemAppend 的差别
前端·后端