【发布时间】:2018-04-22 18:01:09
【问题描述】:
以下是我的 sql 查询的结果:
SELECT DISTINCT(department_id)
from employees
ORDER BY department_id;
结果:
DEPARTMENT_ID
10
20
30
40
50
60
70
80
90
100
110
然后:
SELECT department_id
FROM departments
ORDER BY department_id;
DEPARTMENT_ID
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
270
当我执行以下查询时,我得到了结果:
SELECT department_id
from departments
where department_id IN (select distinct(department_id) from employees)
ORDER BY department_id;
DEPARTMENT_ID
10
20
30
40
50
60
70
80
90
100
110
但是,当我执行以下查询时,以下查询返回“NO Rows Selected”:
SELECT department_id
from departments
WHERE department_id NOT IN (select distinct(department_id) from employees)
ORDER BY department_id;
我所期望的是员工表中不存在的部门 ID。 我觉得这是初学者的错误,但是我无法解决此问题。任何帮助将不胜感激。
【问题讨论】:
-
请显示示例输入数据(表格行值)
-
员工表中是否有任何您未向我们显示的空部门 ID?
-
@Martin Smith Yepp 确实如此!在阅读了下面 Gordon Linoff 的答案后,我明白了这一点。但是,我仍然不清楚为什么一名员工的部门 ID 为空值导致查询没有结果。你能帮我解决这个问题吗?提前致谢
-
我试着在这里解释一下。不知道有多成功...dba.stackexchange.com/questions/48643/…