获取不匹配记录SQL Server表



一个是需求表,另一个表是varientcountry,我只需要从需求表中的thoes变量,而varientcountry表中没有国家映射

需求表

Id Varient Country
1     v1       India
2     v2       NULL
3     v3       Nepal
4     v4       Japan

varientcountry表

Id Varient Country
1    v1       India
2    v1       Uk
3    v2       China
4    v1       Indonisia
5    v3       Nepal
6    v4       Egland
7    v4       Null

我想要

的例外结果
Id Varient Country
1     v1       UK
3     v2       China
6     v4       England

如果没有IDS的生活,则可以使用except

select Varient, Country
from VarientCountry
except
select varient, country
from demand;

如果您需要ID,我会选择not exists

select vc.*
from varientcountry vc
where not exists (select 1
                  from demand d
                  where d.varient = vc.varient and
                        (d.country = vc.country or d.country is null and vc.country is null)
                 );

您必须使用 except一个。

您必须从demandexceptvarientCountry表中选择记录。这是正确的。

   select varient, Country
    from demand
    except
    select varient, country
    from varientCountry;

您将获得以下输出

Id Varient Country
1     v2       Null
2     v4       Japan

相关内容

最新更新