oracle空值参与运算结果就为空

昨天在做一道题目时,发现了一个问题,就是空值的问题。

问题是在oracle自带的emp表的。

emp表有其中的 empno 编号,ename 姓名 mgr 上级编号 三列。

emp表部分数据

题目要求是这样的:

 查询不直接领导同事的员工的信息(即有上司无下属)

我写的答案是select empno,ename from emp where empno not in(select mgr from emp );

本来觉得能得到结果的,可怎么查询也不出结果。我纳闷了,后来我新建了一个表,插了几行测试数据,发现能查到结果。但一到emp表就不出结果。

我准备把emp表的一行行数据插进来,然后每插一行做一次测试,所以能得到结果。就在这时,我看到了一个特殊的数据 mgr有一个数据为null ,眼前突然一亮,是不是它搞的鬼呢,我把它改成1234 然后查询,结果出来了。看来还是null的问题,问了同学,同学告诉我说,null值参与运算结果就会为空的。

我做了一次测试 select * from emp where empno not in(null) 结果查不到数据,看来是真的。发现好像null值只能用于 is null或者 is not null 用其它的运算符比如<> =都会使结果为空。OK,学了一个知识点。

更改语句   select empno,ename,sal,mgr from emp where mgr is not null and empno not in (select mgr from emp where mgr is not null) 结果出来了。

此题还有另一种解法:select ename,sal,mgr from emp where empno not in(select e1.empno from emp e1,emp e2 where e1.empno=e2.mgr) 也可以

原文地址:https://www.cnblogs.com/zwl24/p/2357996.html