如何将SQL子查询转换为连接:
SELECT [COUNTRY] FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc.ss]
WHERE [COUNTRY] NOT IN
(SELECT [EntityAttributeValue] FROM [CountryMappingDB].[dbo].[DNB_MappingData.ss]
UNION ALL
SELECT [SalesCountryName] FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc_dimSalesCountry.ss]);
答案 0 :(得分:1)
我认为编写查询的最佳方式是:
SELECT [COUNTRY]
FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc.ss] ss
WHERE NOT EXISTS (SELECT 1
FROM [CountryMappingDB].[dbo].[DNB_MappingData.ss] ss2
WHERE ss2.County = ss.Country
) AND
NOT EXISTS (SELECT 1
FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc_dimSalesCountry.ss] ss3
WHERE ss3.SalesCountryName = ss.Country
);
虽然您可以使用JOIN
来编写查询,但我认为这更易于理解,并且当比较表具有NULL
值时,它将按预期工作。
答案 1 :(得分:0)
试试这个;
SELECT [COUNTRY] FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc.ss]
left join
(SELECT MyData=[EntityAttributeValue] FROM [CountryMappingDB].[dbo].[DNB_MappingData.ss]
UNION ALL
SELECT MyData=[SalesCountryName] FROM [CountryMappingDB].[dbo].[DNB_Country_mapping_poc_dimSalesCountry.ss]
) countries on [COUNTRY]=countries.MyData
WHERE countries.MyData is null