0
点赞
收藏
分享

微信扫一扫

oracle(21)_SQL_exists 和 not exists 关键字的用法

曾宝月 2022-03-30 阅读 79


SQL

子查询

exists 和 not exists 关键字的用法

范例:查询出有员工的部门有哪些

  • 示例图:
    oracle(21)_SQL_exists 和 not exists 关键字的用法_SQL
    oracle(21)_SQL_exists 和 not exists 关键字的用法_子查询_02

● exists 关键字的用法

  • exists (sql 返回结果集为真)
  • 示例图:
    oracle(21)_SQL_exists 和 not exists 关键字的用法_子查询_03

范例:

● not exists 关键字的用法

  • not exists (sql 不返回结果集为真)
  • 示例图:
    oracle(21)_SQL_exists 和 not exists 关键字的用法_SQL_04

以上操作完整源码:

--查询出有员工的部门有哪些
--in关键字尽量要少使用,因为性能比较低,可以使用 exists 来代替性能很高
select * from dept t where t.deptno in (select distinct deptno from emp);

--exists()子查询的记录数是 0 则整个表达式是 false 如果大于 0 为 true,
--exists 子查询一般是要和外侧查询关联的
select * from emp t where exists (select * from dept d where d.deptno = 50);

--用exists来实现查询出有员工的部门有哪些
select * from dept t where exists (select * from emp e where e.deptno = t.deptno);

--用not exists来实现查询出没有员工的部门有哪些
select * from dept t where not exists (select * from emp e where e.deptno = t.deptno);

如有错误,欢迎指正!



举报

相关推荐

0 条评论