mysql NOT EXISTS
时间: 2023-10-14 20:03:24 浏览: 83
mysql not in、left join、IS NULL、NOT EXISTS 效率问题记录
在MySQL中,NOT EXISTS是一个用于查询的关键字,用于判断子查询的结果集是否为空。当子查询结果为空时,NOT EXISTS返回true,否则返回false。它可以用于过滤掉不符合条件的记录。
例如,如果我们想要查询选修了全部课程的学生姓名,可以使用NOT EXISTS来实现。以下是一个示例查询语句:
```
SELECT Sname FROM Student WHERE NOT EXISTS (
SELECT * FROM Course WHERE NOT EXISTS (
SELECT * FROM SC WHERE Sno=Student.Sno AND Cno=Course.Cno
)
);
```
这个查询语句的含义是:对于每个学生,如果不存在一个课程,使得学生没有选修这门课程,则返回学生的姓名。换句话说,只有选修了全部课程的学生才会被查询出来。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [[Mysql] EXISTS | NOT EXISTS](https://blog.csdn.net/Hudas/article/details/124816473)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [mysql 相关自查查询之 EXISTS 和 NOT EXISTS](https://blog.csdn.net/bbj12345678/article/details/122427903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文