NOT IN有多个select语句

时间:2018-05-15 12:25:24

标签: sql sql-server tsql

我需要执行以下操作,但是T-SQL在NOT IN中有多个select语句的问题

select * from table1 where ParamID not in 
(select paramid from tbl2 
 or 
 select paramid from tbl3 
 or 
 select paramid from tbl3) 

我收到错误:选择

附近的语法不正确

还有另一种方法可以做我想做的事情。

3 个答案:

答案 0 :(得分:3)

使用UNION,例如:

select * from table1 where ParamID not in 
(select paramid from tbl2 
 UNION 
 select paramid from tbl3 
 UNION
 select paramid from tbl3) 

答案 1 :(得分:3)

我会改用NOT EXISTS

select t.* 
from table1 t
where not exists (select 1 from tbl2 where paramid = t.paramid) or
      not exists (select 1 from tbl3 where paramid = t.paramid);

答案 2 :(得分:2)

试试这个..

select * from table1 where ParamID not in 
(select paramid from tbl2 
 union all 
 select paramid from tbl3 
 union all 
 select paramid from tbl3)