这是我写的一条sql语句
select max(sal) from emp
where empno not in (select mgr from emp );
结果是:
  MAX(SAL)
----------在mgr字段中存在null值
改过sql以后,
select max(sal) from emp
where empno not in (select mgr from emp where mgr is not null );
结果是:
  MAX(SAL)
----------
      1600
什么原因,为什么有空值就查不上来

解决方案 »

  1.   

    select mgr from emp where mgr is not null 
    这里有空值,整个值为就是false
      

  2.   

    因为null不能和任何值进行比较(只能用is 或is not),因此not in (null)或not in (null,aaa,bbb)这样的表达式的结果始终是非.
      

  3.   


    select max(sal) from emp
    where empno not in (select mgr from emp where mgr is not null ); 
    一个表里面查?not in在两个表之间才能用的把,感觉这个关系存在两个表:employee和manager,一对多的关系select max(sal) from emp
    where empno not in (select mgr from manager ); 
      

  4.   

    not in的子查询是不能有空记录存在的,否则查询的结果就是空值。以为空和任何值比较都是空。
      

  5.   

    在嵌套查询中下层查询是嵌套在上层查询中的,因此上层查询的where条件后的列应该和下一层查询select后的列相同啊
    select max(sal) from emp 
    where empno not in (select empno from emp where mgr is not null ); 
    刚好你的empno应该是不允许为空的,主键吗?
      

  6.   

    楼主可以重新考虑一下数据库建模,empno和mgr应该是两个表的东西,分别建两个表,manager也是员工,也有员工号,但是这里鼓励单独做成一个表,然后把他跟emp员工表建立1对多关系好办些
    关系清楚了,可以避免null的问题