oracle语句问题

来源:百度知道 编辑:UC知道 时间:2024/05/10 11:25:12
我的是oracle 10g
这个句子是对的
select distinct department_id from departments d
where d.department_id not in (select distinct e.department_id from employees e where
e.department_id is not null);

这个却行不通

select distinct department_id from departments d
where not exists (select distinct e.department_id from employees e
where e.department_id is not null);

必须这样

select distinct department_id from departments d
where not exists (select distinct e.department_id from employees e
where e.department_id <> null);

请告诉我为什么 ?谢谢

呵呵,你把自己搞糊涂了,先编个号吧
1、
select distinct department_id from departments d
where d.department_id not in (select distinct e.department_id from employees e where
e.department_id is not null);

2、
select distinct department_id from departments d
where (select distinct e.department_id from employees e
where e.department_id is not null);

3、select distinct department_id from departments d
where not exists (select distinct e.department_id from employees e
where e.department_id <> null);

4、
select distinct e.department_id from employees e
where e.department_id is not null

5、
select distinct e.department_id from employees e
where e.department_id <> null

首先 2 是可以的
因为你的数据库中 4 是有数据的,再用not exists,那么 2 当然没数据了。
再说 3 为什么可以
因为 5 中e.department_id <> null是个不成立的条件,5 永远不存在数据,没有列可以等于null,若为空值必须用is null或者用nvl函数,否则只能为e.department_id = ‘null’,所以再用not exists,就