mysql exists 和not exists 联合使用的bug

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 

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

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

相关推荐

  1. mysql exists not exists 联合使用bug

    2024-03-26 05:18:07       38 阅读
  2. react之ReducerContext联合使用

    2024-03-26 05:18:07       31 阅读
  3. unity安卓so文件联合使用

    2024-03-26 05:18:07       25 阅读
  4. 关键字联合体union定义使用

    2024-03-26 05:18:07       48 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-26 05:18:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-26 05:18:07       100 阅读
  3. 在Django里面运行非项目文件

    2024-03-26 05:18:07       82 阅读
  4. Python语言-面向对象

    2024-03-26 05:18:07       91 阅读

热门阅读

  1. Spring 实现 OAuth2 授权之解决方案

    2024-03-26 05:18:07       37 阅读
  2. ORA-29548

    ORA-29548

    2024-03-26 05:18:07      37 阅读
  3. C++细节

    C++细节

    2024-03-26 05:18:07      42 阅读
  4. 强化学习:让AI自主学习与决策

    2024-03-26 05:18:07       42 阅读
  5. HTTP协议

    2024-03-26 05:18:07       41 阅读
  6. 消息中间件如何实现高可用

    2024-03-26 05:18:07       46 阅读
  7. 力扣刷题之22.括号生成

    2024-03-26 05:18:07       46 阅读