2014-03-26 54 views
0

通過見下表(如何寫查詢)在Table B查找數據

例獲得Table A數據和status = InActive沒有數據:4 Comm4 InActive

Table A 

AID Name   Status 
-- ---    -- 
1 comm1   Active 
2 comm2   Active 
3 Comm3   InActive 
4 Comm4   InActive 
5 Comm5   InActive 


Table B 

BID Name AID 
--- ---- --- 
    11 James 1 
    12 Kris 2 
    13 Dan 3 
    14 Steve 3 
    15 Brian 5 

回答

1

這是很簡單的

select * from tableA 
where status = 'InActive' 
and not exists (select * from tableB where tableA.AID = tableB.AID) 
0
select tableA.* 
    from tableA 
    left join tableB 
    on tableA.AID = tableB.AID 
    and tableA.status = 'InActive' 
where tableB.AID is null 

的不存在Szymon是正確的,可能更有效率

-1

在這裏你嘗試。

select * 
from #table_one 
where Status = 'InActive' 
and not exists 
(
    select 1 from #table_two where AID = #table_one.AID 
);