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

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

相关推荐
川石课堂软件测试35 分钟前
软件测试|常见面试题整理
数据库·python·jmeter·mysql·appium·postman·prometheus
这个昵称也不能用吗?1 小时前
eas 热更新相关
前端
KaMeidebaby1 小时前
卡梅德生物技术快报|葫芦科植物遗传转化:Fast‑TrACC 工程化优化:葫芦科植物遗传转化效率提升与成本控制
前端·其他·百度·新浪微博
换日线°2 小时前
vue 加入购物车抛物线动画
前端·javascript·vue.js
薪火铺子2 小时前
MySQL 锁机制与死锁分析深度解析
数据库·mysql
千百元2 小时前
mysql5.7 定时删除表数据
mysql
切糕师学AI2 小时前
为什么你的 SPA 网址必须包含 `#`?—— 前端路由 Hash 模式深度解析
前端·spa 网址·hash路由
冴羽2 小时前
超越Vibe Coding —— AI 辅助编程进阶指南
前端·javascript·ai编程
MXN_小南学前端3 小时前
自制和整理常用前端 AI Skills分享,从需求到页面(附github地址)
前端·ai编程
yuki_uix3 小时前
双 RAF + MutationObserver:微前端跳转后的滚动复原完整方案
前端