2012-09-21 195 views
2

我使用Microsoft SQL Server和我查詢的格式如下(表1主鍵(A,B))的:SQL查詢 - 過濾主鍵

Select table1.A, table1.B, table2.C, Table3.D 
from table1 
left outer join table2 on table1.A = table2.A 
left outer join tabl3 on table1.B = table3.B 

//Here comes the question 
//Except (A,B) in (select A,B from BadTable) 

我怎麼能做到這一點很好?我在想找到一套使用某物像正確的鍵的(不能使用除外 - 運行SQL Server 2000)

Select A,B 
from table1 
not exists (select A,B from bad table) 

,然後內部聯接與主查詢。你怎麼看?

回答

1
Select table1.A, table1.B, table2.C, Table3.D 
from table1 T1 
left outer join table2 on table1.A = table2.A 
left outer join tabl3 on table1.B = table3.B 
where not exists (select 1 from badTable where A = T1.A and B = T1.B) 

將是一個很好的方式

0
Select table1.A, table1.B, table2.C, Table3.D ,badtable.* 
from table1 
left join table2 on table1.A = table2.A 
left join table3 on table1.B = table3.B 
left join badtable on table1.a = badtable.a 
    and table1.b = badtable.b 
where badtable.a is null