2017-10-18 60 views
2

我命名为tmptable1tmptable2 两个SQL数据表我想显示在tmptable1数据,但不能在tmptable2 我已经写了下面的查询,但它显示空白的结果,但我知道,有一条记录在tmptable1但不在tmptable2 以下是我的查询。我在做什么错。查找不匹配之间的两个SQL数据表

select * from tmptable1 where name not in(select name from tmptable2 where status='active') 
+0

喜我使用MS SQL – Mithu

+0

u能请注明完整查询 – Mithu

+0

样本数据可以是名称,地址,城市,mobilno和国家 – Mithu

回答

3

你也可以采取的EXCEPT and INTERSECT优势:

下面给你的名字存在于tmptable1但不是在tmptable2

SELECT name FROM tmptable1 

EXCEPT 

SELECT name FROM tmptable2 

而这给你的通用名称:

SELECT name FROM tmptable1 

INTERSECT 

SELECT name FROM tmptable2 
+0

嗨thnks其工作像一个魅力 – Mithu

1

一种方法是使用NOT EXISTS与相关子查询:

select * 
from tmptable1 t1 
where not exists (
    select 1 
    from tmptable2 t2 
    where t1.name = t2.name 
    and t2.status = 'active' 
    ); 

或者,你可以使用LEFT JOIN

select t1.* 
from tmptable1 t1 
left join tmptable2 t2 
    on t1.name = t2.name 
    and t2.status = 'active' 
where t2.name is null; 
相关问题