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

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

相关推荐
yingyima几秒前
正则表达式实战:如何高效清洗脏数据
前端
兔子零10242 分钟前
Ofox AI值得用吗?
前端·javascript·后端
俺不要写代码27 分钟前
数据库:DQL
数据库·sql·mysql
We་ct34 分钟前
React 性能优化精讲
前端·javascript·react.js·性能优化·前端框架·html·浏览器
云动课堂44 分钟前
【运维实战】Nginx 高性能Web服务 · 一键自动化部署方案 (适配银河麒麟 V10 / openEuler / CentOS 7/8)
运维·前端·nginx
小碗羊肉2 小时前
【MySQL | 第十一篇】InnoDB引擎
java·数据库·mysql
大前端helloworld2 小时前
AI全自动实现Flutter蓝牙自动连接
前端
xxjj998a2 小时前
Laravel8.x核心特性详解
数据库·mysql·adb
GISer_Jing2 小时前
从入门到落地:前端开发者的AI Agent全栈学习路线
前端·人工智能·ai编程
计算机安禾2 小时前
【Linux从入门到精通】第47篇:SystemTap与eBPF——Linux内核观测的显微镜
java·linux·前端