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

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

相关推荐
Apifox7 分钟前
Apifox CLI + Claude Skills:将接口自动化测试融入研发工作流
前端·后端·测试
程序员Agions25 分钟前
别再只会 console.log 了!这 15 个 Console 调试技巧,让你的 Debug 效率翻倍
前端·javascript
我的div丢了肿么办28 分钟前
vue使用h函数封装dialog组件,以命令的形式使用dialog组件
前端·javascript·vue.js
UIUV29 分钟前
Git 提交规范与全栈AI驱动开发实战:从基础到高级应用
前端·javascript·后端
NEXT0630 分钟前
那个写 width: 33.33% 的前端,终于被 flex: 1 拯救了
前端·css
NEXT0632 分钟前
前端即导演:用纯 CSS3 原力复刻《星球大战》经典开场
前端·css
tc&39 分钟前
为什么 Kamailio 模块封装的 MySQL 函数能有效防范 SQL 注入?
数据库·sql·mysql·网络攻击模型·kamailio
cookqq43 分钟前
Java+MySQL时区难题-Date自动转换String差8小时
数据库·mysql