问题在于,在您的问题要求中,您似乎需要将null 视为等于(其他)null。
这违背了null 的定义。但是你可以达到你需要的结果——在加入条件下,改变
and a.subject = b.subject
到
and (a.subject = b.subject or (a.subject is null and b.subject is null))
在 Oracle 中,您可以更简洁地做到这一点:
and decode (a.subject, b.subject, 1) = 1
因为decode 被有意定义为将两个null 视为“相等”。
如果需要,您可以对 id 列执行相同操作 - 但假设它应该是两个表中的主键,则不需要它。
编辑 OP 声称decode 的建议使用会产生错误的结果,而or (a.subject is null and b.subject is null) 的显式使用会在SQL Developer 上无限期运行。因此,在此编辑中,我将表明 OP 的声明都是错误的。 (我在我的 SQL Developer 副本上运行了所有内容,所以 IDE 不是问题。)
他在做什么,我们不知道,但是对于所提供的数据和给我们的查询,这两个建议都是 100% 正确的(在我们添加了 OP 查询中缺少的表别名之后,我们仅选择来自TableA 的数据)。
测试数据:
create table tablea (id, name, subject) as
select 1, 'tom' , 'math' from dual union all
select 2, 'jack', null from dual
;
create table tableb (id, name, subject) as
select 3, 'john', 'science' from dual union all
select 2, 'jack', null from dual
;
第一个查询和结果:
SELECT a.*
FROM TableA a
LEFT JOIN TableB b
ON a.id = b.id
AND (a.subject = b.subject or (a.subject is null and b.subject is null))
WHERE b.id IS NULL
AND b.subject IS NULL
;
ID NAME SUBJECT
---------- ---- -------
1 tom math
第二次查询和结果:
SELECT a.*
FROM TableA a
LEFT JOIN TableB b
ON a.id = b.id
AND decode(a.subject, b.subject, 1) = 1
WHERE b.id IS NULL
AND b.subject IS NULL
;
ID NAME SUBJECT
---------- ---- -------
1 tom math