Oracle Study之---Oracle IN和NOT IN的使用

NOT IN 与  IN 的区别:

------------------------------------------------------------------------------------------------------

not In 相当于<> all,如果 Not In 后面跟的是子查询的话,子查询中只要包含一个 null 的返回值,则会造成

整个 Not in 字句返回空值,结果就是查询不会返回任何结果。

而 in 相当于=any的意思,可以有效处理子查询中返回空值的情况,返回正确的结果。

------------------------------------------------------------------------------------------------------

NOT IN示例:

--该例子想要返回没有下属的职员的姓名,如果子查询中有空值返回的话,则整个查询将没有结果返回11:20:02 SYS@ test3 >conn scott/tiger

Connected.

11:21:18 SCOTT@ test3 >select * from emp;

EMPNO ENAME      JOB              MGR HIREDATE                   SAL       COMM     DEPTNO

---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------

7369 SMITH      CLERK           7902 1980-12-17 00:00:00        800                    20

7499 ALLEN      SALESMAN        7698 1981-02-20 00:00:00       1600        300         30

7521 WARD       SALESMAN        7698 1981-02-22 00:00:00       1250        500         30

7566 JONES      MANAGER         7839 1981-04-02 00:00:00       2975                    20

7654 MARTIN     SALESMAN        7698 1981-09-28 00:00:00       1250       1400         30

7698 BLAKE      MANAGER         7839 1981-05-01 00:00:00       2850                    30

7782 CLARK      MANAGER         7839 1981-06-09 00:00:00       2450                    10

7788 SCOTT      ANALYST         7566 1987-04-19 00:00:00       3000                    20

7839 KING       PRESIDENT            1981-11-17 00:00:00       5000                    10

7844 TURNER     SALESMAN        7698 1981-09-08 00:00:00       1500          0         30

7876 ADAMS      CLERK           7788 1987-05-23 00:00:00       1100                    20

7900 JAMES      CLERK           7698 1981-12-03 00:00:00        950                    30

7902 FORD       ANALYST         7566 1981-12-03 00:00:00       3000                    20

7934 MILLER     CLERK           7782 1982-01-23 00:00:00       1300                    10

14 rows selected.

11:20:11 SCOTT@ test3 >select empno from emp

11:20:21   2            where empno NOT IN (select mgr from emp);

no rows selected

说明:

Null Values in a Subquery

The SQL statement in the slide attempts to display all the employees who do not have any

subordinates. Logically, this SQL statement should have returned 12 rows. However, the SQL

statement does not return any rows. One of the values returned by the inner query is a null value and,

therefore, the entire query returns no rows

The reason is that all conditions that compare a null value result in a null. So whenever null values

are likely to be part of the resultsset of a subquery, do not use the NOT INoperator. The NOT IN

operator is equivalent to <> ALL.

---------------------------------------------------------------------------------------------------------

IN的示例:

Notice that the null value as part of the results set of a subquery is not a problem if you use the IN

operator. The IN operator is equivalent to =ANY. For example, to display the employees who have

subordinates(下属), use the following SQL statement:11:20:42 SCOTT@ test3 >select empno from emp

11:21:04   2      where empno in  (select mgr from emp);

EMPNO

----------

7566

7698

7782

7788

7839

7902

6 rows selected.

---------------------------------------------------------------------------------------------------------Alternatively, a WHERE clause can be included in the subquery to display all employees who do nothave any subordinates:--使用 Not In 的话,要注意除掉子查询中将要返回的空值11:27:01 SCOTT@ test3 >select empno from emp

11:27:12   2  where empno NOT IN (select mgr from emp WHERE MGR IS NOT NULL);

EMPNO

----------

7844

7521

7654

7499

7934

7369

7876

7900

8 rows selected.

oracle什么时候用in,Oracle Study之---Oracle IN和NOT IN的使用相关推荐

  1. Oracle Study之--Oracle等待事件(3)

    Oracle Study之--Oracle等待事件(3) Db file parallel read 这是一个很容易引起误导的等待事件,实际上这个等待事件和并行操作(比如并行查询,并行DML)没有关系 ...

  2. oracle顺序读等待,Oracle Study之--Oracle等待事件(4)

    Oracle Study之--Oracle等待事件(4) Db file scattered read这个等待事件在实际生产库中经常可以看到,这是一个用户操作引起的等待事件,当用户发出每次I/O需要读 ...

  3. Oracle Study之--Oracle 11g RAC设置归档路径错误案例

    Oracle Study之--Oracle 11g RAC置归档路径错误案例 系统环境: 操作系统: RedHat EL55 集群:     Oracle 11g GI Oracle:   Oracl ...

  4. Oracle Study之--Oracle 11gR2通过RMAN克隆数据库

    Oracle Study之--Oracle 11gR2通过RMAN克隆数据库 Purpose of Database Duplication A duplicate database is usefu ...

  5. Oracle Study之--Oracle 11g RAC添加节点错误

    Oracle Study之--Oracle 11g RAC添加节点错误 系统环境:     操作系统:RedHat EL5     Cluster:  Oracle 11gR2 Grid     Or ...

  6. oracle 11g ocm 题目,Oracle Study之-Oracle 11g OCM考试(1)

    Oracle Study之-Oracle 11g OCM考试(1) 系统环境:RedHat EL64 Oracle:   Oracle 11gR2 Oracle 11g OCM考试第一题,手工建库,参 ...

  7. Oracle Study之--Oracle 11g RAC故障(Failed to create or upgrade OLR)

    系统环境: 操作系统: RedHat EL5.5 Cluster:   Oracle Grid 11.2.0.1.0 Oracle:     Oracle 11g  11.2.0.1.0 故障现象:  ...

  8. oracle重建实例_记一次误删Oracle控制文件并恢复过程

    概述 当你在数据库运行时误删除了控制文件怎么办?很不幸有一次我就有这个情况,虽然是测试环境,这里因为我有事先把控制文件分别备份,所以恢复还是比较简单的.下面简单记录下怎么恢复. 问题 控制文件版本不一 ...

  9. oracle for 记录数,可视化工具dbForge Documenter for Oracle全新上线!让您轻松记录Oracle数据库...

    dbForge Documenter for Oracle是一个用于记录Oracle数据库的便捷可视化工具.此Oracle文档工具以HTML,PDF和MARKDOWN文件格式自动生成整个Oracle数 ...

最新文章

  1. 初学MyBatis-Plus
  2. R语言ggplot2可视化:使用R原生plot函数为指定曲线下面的区域着色、ggplot2可视化在曲线的特定下方添加分割线、ggplot2为指定曲线下面的区域着色
  3. iOS-iOS9.Plist插入网络安全xml
  4. ubuntu安装Java开发环境
  5. 中采购订单批导的bapi_跟着团子学SAP PS—项目中的物料组件
  6. jsoncpp和rapidjson哪个好用?
  7. ROS : 修改ROS源代码(overlaying package)
  8. 树莓派添加USB外接硬盘
  9. 文件那些事儿之一(初稿)
  10. 谷歌开源文件访问漏洞审计工具 PathAuditor(详解)
  11. python dataframe的某一列变为list_Python数据分析系列文章之Pandas(上)
  12. Riverbed“不再只是一家广域网优化公司”
  13. Git 常用命令 和 安装
  14. 服务器内存傲腾基本参数信息,服务器傲腾内存
  15. 如何成为成功的Andr​​oid游戏开发
  16. 如何将cad导成jpg图片格式?
  17. 怎样看oracle删掉没有,肿么查看以前Oracle卸载干净没
  18. 基于java的URP教务系统爬虫实现
  19. android MediaPlayer SurfaceView 网络视频播放器
  20. yahoo.cn邮箱foxmail收发攻略

热门文章

  1. 如何让css与js分离
  2. English trip -- VC(情景课)5 Around Town
  3. Beego 学习笔记9:Boostrap使用介绍
  4. 单行文本与多行文本省略文本
  5. Git从零教你入门(4):Git服务之 gogs部署安装
  6. Robot Framework: 自定义自己的python库
  7. 浮动在IE6,7下的一些问题
  8. jquery获取select中的option的text值
  9. 并发服务器设计思路,参考apache学习UDP和QoS,研究成果
  10. 订单拣选作业模式总结