Oracle SQL in 和 not in both return 空
Oracle SQL in and not in both return empty
我有一个包含用户名列的人员 table。
我还有一个包含用户名列的学生 table。
“David”存在于人 table 中,但不存在于学生 table.
当我运行这个时,我没有得到不正确的结果:
Select USERNAME
From PEOPLE
Where username = 'david' and USERNAME not in (Select USERNAME From STUDENT)
Order By USERNAME
但是,当我运行这样的时候,我也没有得到任何结果。这怎么可能?
Select USERNAME
From PEOPLE
Where username = 'david' and USERNAME in (Select USERNAME From STUDENT)
Order By USERNAME
如果 student
中的任何 USERNAME
是 NULL
,则表达式的计算结果为 NULL
。过滤掉 all 行。我猜这是你的问题。
要解决此问题,我建议 never 将 NOT IN
与子查询一起使用。只需习惯使用 NOT EXISTS
:
where username = 'david' and
not exists (select 1 from student s where s.USERNAME = people.username)
我有一个包含用户名列的人员 table。 我还有一个包含用户名列的学生 table。 “David”存在于人 table 中,但不存在于学生 table.
当我运行这个时,我没有得到不正确的结果:
Select USERNAME
From PEOPLE
Where username = 'david' and USERNAME not in (Select USERNAME From STUDENT)
Order By USERNAME
但是,当我运行这样的时候,我也没有得到任何结果。这怎么可能?
Select USERNAME
From PEOPLE
Where username = 'david' and USERNAME in (Select USERNAME From STUDENT)
Order By USERNAME
如果 student
中的任何 USERNAME
是 NULL
,则表达式的计算结果为 NULL
。过滤掉 all 行。我猜这是你的问题。
要解决此问题,我建议 never 将 NOT IN
与子查询一起使用。只需习惯使用 NOT EXISTS
:
where username = 'david' and
not exists (select 1 from student s where s.USERNAME = people.username)