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

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

相关推荐
皮皮林5518 小时前
CTO说MySQL单表行数不要超过2000w,为啥?
mysql
ZhengEnCi11 小时前
前端日志和后端日志怎么记:分级、JSON 格式、按天分文件与保留策略
前端·后端·python
codeejun12 小时前
每日一Go·MySQL-24、死锁排查与解决方案
开发语言·mysql·golang
kyriewen12 小时前
我给 61 个前端库排了两次名,最多差了 46 位
前端·javascript·程序员
亲亲小宝宝鸭13 小时前
Cesium启动:TLE数据是如何变成轨迹线的
前端·cesium
阿里云云原生13 小时前
对话即构建,在 AgentCore 构建 Agent 就是这么简单
前端·agent
夕水14 小时前
我给我的诗写了个后台,顺手写了 一个组件库ew-solid-ui
前端·javascript·typescript
狗头大军之江苏分军14 小时前
前端转行干能干什么?我转行试了下(被迫)
前端·后端
孟陬14 小时前
Foobar 技术周刊#2:约会已死?用户急剧下滑,约会App大概率会弱化约会属性转向轻陪伴。我有个建议:
前端·后端·rust
码农张315 小时前
MySQL 8.4.13 LTS 压缩版安装步骤 + 规范配置文件
mysql