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 小时前
图片标签用 img 还是 picture?很多人彻底弄混了!
前端·html
lichong9516 小时前
XLog debug 开启打印日志,release 关闭打印日志
android·java·前端
IT油腻大叔6 小时前
MySQL VS ClickHouse 索引结构对比分析
mysql·clickhouse
烟袅6 小时前
作用域链 × 闭包:三段代码,看懂 JavaScript 的套娃人生
前端·javascript
3***31216 小时前
初识MySQL · 库的操作
数据库·mysql
anod6 小时前
奇怪的mysql时区问题
数据库·mysql·eclipse
鲸说MySQL6 小时前
MySQL表文件损坏
数据库·mysql
风止何安啊7 小时前
收到字节的短信:Trae SOLO上线了?尝尝鲜,浅浅做个音乐播放器
前端·html·trae
抱琴_7 小时前
大屏性能优化终极方案:请求合并+智能缓存双剑合璧
前端·javascript
用户463989754327 小时前
Harmony os——长时任务(Continuous Task,ArkTS)
前端