Oracle 中子查询

来源:百度知道 编辑:UC知道 时间:2024/06/18 02:37:55
select max(sal) from emp wher empno not in
(select distinct mgr from emp where mgr is not null)

该语句是把 select distinct mgr from emp where mgr is not null 当成一个表 我要问的就是··

select distinct mgr from emp where mgr is not null 中 没有返回 empno

那 wher empno not in 怎么执行呀?

是不是应该改成下面的语句
select max(sal) from emp wher empno not in
(select empno,distinct mgr from emp where mgr is not null) 才是对的呀·

原句 是我们老师讲的 我感觉不对 可是执行了是对的

所以麻烦哪位高手指点一下
不懂 谁还能解释的清楚一些·

因为[select distinct mgr from emp where mgr is not null]的返回结果是一个数据集,只要返回的类型和empno一样,或者empno是char型,返回结果全是数字,就能够检索到结果。
*********如:********
假设empno是char型,mgr是number或者是char型。
[select distinct mgr from emp where mgr is not null]检索到结果(1,2,3,4,5),那么empno不等于(1,2,3,4,5)的值就会全部出来。
*********关于你写的***
因为外面写的是一个字段empno,你里面写的是两个,一个是没有办法和两个相等的。下面是oracle执行结果:
SQL> desc UserInfo
Name Null? Type
----------------------------------------- -------- ----------------------------
NAME VARCHAR2(20)
CHINESE NUMBER(38)

SQL> select NAME from UserInfo where name not in(select NAME from UserInfo);

no rows selected

SQL> select NAME from UserInfo where name not in(select * from UserInfo);
select NAME from UserInfo where name not in(select * from UserInfo)