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

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

相关推荐
hqk8 分钟前
我给 AI 造了一个原型工具:AIWireframeViewer V1
前端·ai编程·vibecoding
Highcharts.js16 分钟前
商业授权与开源图表库如何选择?
前端·javascript·开源·highcharts·chart.js·echarts.js
工具派26 分钟前
写代码时,我把这个本地 AI Commit Message 生成器接进了工作流
前端·后端
LaughingZhu42 分钟前
Product Hunt 每日热榜 | 2026-07-15
前端·人工智能·神经网络·react.js·搜索引擎·前端框架
G.O.G.O.G1 小时前
《LeetCode SQL 从入门到精通(MySQL)》09
sql·mysql·leetcode
️学习的小王1 小时前
MySQL 数据库基础操作全解:从建库到增删改查
数据库·mysql·oracle
掘金者阿豪1 小时前
从Oracle迁到金仓,第一个月踩的最大的坑就是它
前端·后端
AZaLEan__1 小时前
CSS文档流相关:BFC
前端·css
小林ixn2 小时前
Vue3接入DeepSeek流式输出,把“二进制碎片”和“局部热更新”扒个精光
前端·vue.js·人工智能
2501_912784082 小时前
Taocarts 前端国际化实战:多语言与多币种怎么落地不翻车
前端·多语言·taocarts