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

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

相关推荐
JeffongTan6 小时前
在LWC中镶嵌VF Page获取用户IP
前端·javascript·salesforce
91刘仁德7 小时前
MYSQL 事务原理及使用
android·mysql·adb
wxwx_bscxy3228 小时前
NodeJS 高校学业预警系统10551
mysql·node.js·vue·高校学业预警
陆枫Larry8 小时前
Astro 是什么
前端
是立不是利10 小时前
写给设计师的 CSS 博客美学指南
前端·css
默_笙11 小时前
🏝 Docker 就是"房地产开发":从施工图纸到小区物业的容器化指南
前端·javascript
计算机魔术师11 小时前
Rohan Paul 谈用时间跨度衡量智能体能力,并引用 OpenAI 自动化研究实习生里程碑
前端
kyriewen11 小时前
我手写了个极简版 React Router——才搞懂 v6 为什么砍了这么多 API
前端·javascript·程序员
a11177611 小时前
Shirone 二次元博客主题 开源
前端·开源
IT_陈寒11 小时前
Python的切片赋值把我坑惨了,这不是bug是特性
前端·人工智能·后端