2012-09-21 142 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