我需要在Access中执行FULL OUTER JOIN,因此我在Stack Overflow上找到了一个线程,该线程为我提供了以下模型:
SELECT *
FROM AA
LEFT JOIN BB ON AA.C_ID = BB.C_ID AND AA.Currency = BB.Currency
UNION
SELECT *
FROM AA
RIGHT JOIN BB ON AA.C_ID = BB.C_ID AND AA.Curency = BB.Currency
因此,起初它似乎可以工作(大约95%的结果匹配),但是当我检查一列的总和时,它略有变化(就我而言,我有80万行,因此更改非常重要)
此外,每个表中的某些ID为NULL,并且具有ID NULL的每一行都必须在完全外部联接之后位于不同的行上。 我认为有些行,你知道怎么了吗?
预先感谢
答案 0 :(得分:0)
我建议创建所有ID /货币组合的列表,然后使用left join
s:
SELECT AA.*, BB.*
FROM ((SELECT AA.C_ID, AA.Currency FROM AA
UNION -- on purpose to remove duplicates
SELECT BB.C_ID, BB.Currency FROM BB
) as IDS LEFT JOIN
AA
ON AA.C_ID = IDS.C_ID AND
AA.Currency = IDS.Currency
) LEFT JOIN
BB
ON AA.C_ID = BB.C_ID AND
AA.Currency = BB.Currency;
NULL
值对完全联接构成了问题。与SQL中FULL JOIN
的定义一样,NULL
的值不匹配。您可以扩展逻辑以比较NULL
值:
SELECT AA.*, BB.*
FROM ((SELECT AA.C_ID, AA.Currency FROM AA
UNION -- on purpose to remove duplicates
SELECT BB.C_ID, BB.Currency FROM BB
) as IDS LEFT JOIN
AA
ON (AA.C_ID = IDS.C_ID OR AA.C_ID IS NULL AND IDS.C_ID IS NULL) AND
(AA.Currency = IDS.Currency OR AA.CURRENCY IS NULL AND IDS.CURRENCY IS NULL)
) LEFT JOIN
BB
ON (BB.C_ID = IDS.C_ID OR BB.C_ID IS NULL AND IDS.C_ID IS NULL) AND
(BB.Currency = IDS.Currency OR BB.CURRENCY IS NULL AND IDS.CURRENCY IS NULL);