如何在合并结果中使用NOT IN

时间:2018-08-17 07:54:02

标签: sql

我正在尝试从具有不同条件的一张表中获得不同的值。

我正在使用的查询是

Select A.* 
from A
 where A.x not in (select B.x from B 
                   union
                   select C.x from C
                  )

请注意,我一直在使用CTE来获取不同的值。

我独立运行时,select B.x from B union select C.x from C
我看到输出。 表A也是如此。

但是当我一起运行它时,我看不到任何数据。

2 个答案:

答案 0 :(得分:2)

一种方法是使用not exists

Select A.* from A where 
not exists (select 1 from b where b.x = A.x)
and
not exists (select 1 from c where c.x = A.x)

答案 1 :(得分:0)

;with tabletemporary
AS 
( 
    SELECT b.x AS name from b UNION SELECT c.x AS name FROM c
)
Select a.* FROM a WHERE a.x NOT IN
          (SELECT name FROM tabletemporary)

您可以使用此代码

相关问题