2017-10-18 83 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; 
相關問題