我想从表A中选择与以下查询不匹配的数据。 换句话说,除了以下记录之外,我想要表A中的所有内容。 我假设它是一个左连接,但我收到的预期比预期更多。
SELECT distinct
a.Address_Direction,
a.Address_Extension,
a.Address_Number,
a.Address_Root,
a. PHY_ADDR1,
b.PHY_ADDR1
FROM [RD_GeoCode].[dbo].[PA_Stg_Test2] AS a
inner JOIN [RD_GeoCode].[dbo].[rg_ApplicationData_AllForms_20160401_address] AS b ON
B.address_direction = a.address_direction
and B.address_root = a.address_root
and B.address_number = a.address_number
and B.Address_Extension = a.Address_Extension
and b.PHY_ZIPCD = a.PHY_ZIPCD
答案 0 :(得分:2)
使用左连接并添加Where B.AnyField Is Null
如注释,B.AnyField必须是没有Null的列,如主键,否则您将得到误报。
另外请确保您的加入条件不允许重复,因为这会导致您的号码丢失。
答案 1 :(得分:0)
与左连接一起,您必须检查B中的相应条目是否为空。请尝试以下查询:
SELECT distinct a.Address_Direction, a.Address_Extension, a.Address_Number, a.Address_Root, a. PHY_ADDR1, b.PHY_ADDR1
FROM [RD_GeoCode].[dbo].[PA_Stg_Test2] AS a
LEFT JOIN [RD_GeoCode].[dbo].[rg_ApplicationData_AllForms_20160401_address] AS b ON
B.address_direction = a.address_direction
AND B.address_root = a.address_root
AND B.address_number = a.address_number
AND B.Address_Extension = a.Address_Extension
AND b.PHY_ZIPCD = a.PHY_ZIPCD
WHERE B.address_number IS NULL