INNER 和 OUTER JOIN 多个可空列

INNER And OUTER JOIN On Multiple Nullable Columns

我们当前正在测试的查询给出了可疑的结果,其中 Inner JOIN + LEFT OUTER JOIN != B Count.

两个表中使用的比较列都是可空的,但我们的目标很明确:

In case of INNER JOIN or intersection, Nulls should not be used for comparaison.
In case of LEFT OUTER JOIN, if both Bs are Nulls, should be included as not found in A, otherwise, we should compaire only the Not-Nulls.

相交

SELECT b.c1,b.c2
FROM B as b
Inner JOIN A as a
ON (( b.c1= a.c1 and not a.c1 is null)
  OR (b.c2=a.c2 and not a.c2 is null))

当其中一列为空时,前面的代码给出了重复的值!

在 B 不在 A

SELECT b.c1,b.c2
FROM B as b
LEFT OUTER JOIN A as a
ON (( b.c1= a.c1 and not a.c1 is null)
      OR (b.c2=a.c2 and not a.c2 is null))
WHERE a.id IS NULL
-- filter duplicates
GROUP BY b.c1,b.c2

我想问题出在我们连接表的 ON 部分。

提前致谢

如果您愿意,您可以 运行 使用 sub-queries 而不是连接。

SELECT b.c1,b.c2
FROM B as b
WHERE
b.c1 IN (SELECT c1 from a WHERE c1 IS NOT NULL)
OR (b.c2 IN (SELECT c2 from a WHERE c2 IS NOT NULL);