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

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

相关推荐
winfredzhang2 分钟前
打造专属桌面时钟:纯HTML实现的全功能动态时钟
前端·html·农历·生肖·周次
哥本哈士奇8 分钟前
使用Gradio构建AI前端 - RAG的QA模块
前端·人工智能·状态模式
扶我起来还能学_13 分钟前
Vue3 proxy 数据响应式的简单实现
前端·javascript·vue
Dragon Wu31 分钟前
前端项目架构 项目格式化规范篇
前端·javascript·react.js·前端框架
QQ 313163789034 分钟前
文华财经软件指标公式期货买卖信号提示软件
java·前端·javascript
老华带你飞37 分钟前
房屋租赁管理系统|基于java+ vue房屋租赁管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
惜晨宝贝1 小时前
文件上传格式限制
前端·html5·上传测试
IT_陈寒1 小时前
Vue3性能优化实战:7个被低估的Composition API技巧让渲染提速40%
前端·人工智能·后端
颖风船2 小时前
膨胀腐蚀及底层实践,拒绝opencv,matlab等方式
前端·opencv·matlab
俩毛豆2 小时前
【毛豆工具集】【文件】【目录操作】生成沙盒目录
前端·javascript·鸿蒙