如何创建查询以提取包含空值的两个表之间的差异?

时间:2018-07-06 22:35:36

标签: sql ms-access access-vba

我正在编写一个查询,该查询提取了新表和旧表之间的差异。

SELECT New.Entity, New.Type, New.Address, New.Country
FROM tbl_EntityList_New As New
WHERE NOT EXISTS (SELECT * FROM tbl_EntityList_Old as Old
WHERE New.Entity = Old.Entity
AND New.Address = Old.Address);

但是,查询似乎错误地返回了空白地址。为了解决这个问题,我在第一个WHERE子句中添加了另一部分,如下所示:

SELECT New.Entity, New.Type, New.Address, New.Country
FROM tbl_EntityList_New As New
WHERE NOT EXISTS (SELECT * FROM tbl_EntityList_Old as Old
WHERE New.Entity = Old.Entity
AND New.Address = Old.Address)
AND New.Address Is Not Null;

但是,此查询中将排除具有空白地址的新记录,并且我对用户输入新记录的方式没有影响。

如何修改SQL以提取所需的差异?感谢您的提前帮助!

1 个答案:

答案 0 :(得分:1)

我认为您正在寻找:

SELECT New.Entity, New.Type, New.Address, New.Country
FROM tbl_EntityList_New As New
WHERE NOT EXISTS (SELECT 1
                  FROM tbl_EntityList_Old as Old
                  WHERE New.Entity = Old.Entity AND
                        ( New.Address = Old.Address OR
                          New.Address IS NULL AND Old.Address IS NULL
                        );