最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Oracleexistsandnotexist

来源:动视网 责编:小采 时间:2020-11-09 12:13:43
文档

Oracleexistsandnotexist

Oracleexistsandnotexist:先看下面的例子:oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists: 这条语句返回select * from scott.dep 先看下面的例子:Oracle中两个系统表.emp,dept.example: 1:not exists(not in) not
推荐度:
导读Oracleexistsandnotexist:先看下面的例子:oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists: 这条语句返回select * from scott.dep 先看下面的例子:Oracle中两个系统表.emp,dept.example: 1:not exists(not in) not


先看下面的例子:oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists: 这条语句返回select * from scott.dep

先看下面的例子:Oracle中两个系统表.emp,dept.example:

1:not exists(not in)

not exists:

这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的结果集.也就是说,

返回的结果集中不存在d.deptno=10结果集的记录,即emp表中没有dept表中d.deptno=10的记录.

SQL> select empno,ename,deptno from scott.emp e where not exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20

11 rows selected

not in:

第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).

SQL> select empno,ename,deptno from scott.emp e where deptno not in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME DEPTNO
----- ---------- ------
7369 SMITH 20
7499 ALLEN 30
7521 WARD 30
7566 JONES 20
7654 MARTIN 30
7698 BLAKE 30
7788 SCOTT 20
7844 TURNER 30
7876 ADAMS 20
7900 JAMES 30
7902 FORD 20

11 rows selected

2: exists(in)

exists:

这条语句返回select * from scott.dept d where e.deptno=d.deptno and d.deptno=10条件满足的记录结果集.

也就是说返回的结果集中只存在有d.deptno=10的记录,即emp表中只存在dept表中d.deptno=10的记录.

SQL> select empno,ename,deptno from scott.emp e where exists(select * from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10

in:

第一个where条件必须给定字段(deptno), 第二个sql语句中,必须明确给定要查询的字段是哪个(deptno).

SQL> select empno,ename,deptno from scott.emp e where deptno in(select deptno from scott.dept d where e.deptno=d.deptno and d.deptno=10);

EMPNO ENAME DEPTNO
----- ---------- ------
7782 CLARK 10
7839 KING 10
7934 MILLER 10


oracle 中exists (in)和not exists(not in)是判断是否存在和不存在表中记录的关键子.

请注意:not in 逻辑上不完全等同于not exists,如果你误用了not in,小心你的程序存在致命的BUG:因此,请尽量不要使用not in(它会调用子查询),而尽量使用not exists(它会调用关联子查询)。

文档

Oracleexistsandnotexist

Oracleexistsandnotexist:先看下面的例子:oracle中两个系统表.emp,dept.example:1:not exists(not in)not exists: 这条语句返回select * from scott.dep 先看下面的例子:Oracle中两个系统表.emp,dept.example: 1:not exists(not in) not
推荐度:
标签: and oracle notexists
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top