选择某些值不在其他表中的行

时间:2013-07-19 15:13:38

标签: sql oracle

表T3的A和B与T1的A和B相同。 基本上我需要做的是选择T3上没有的所有值。 如果在T3上有一行A,B,我不想表现出来。

SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND NOT EXISTS
(
   SELECT T3.A, T3.B
   FROM T3
)

有任何帮助吗? 感谢

4 个答案:

答案 0 :(得分:4)

SELECT T1.A, T1.B, T1.C
FROM T1 INNER JOIN T2 ON T1.X=T2.X
WHERE NOT EXISTS
(
   SELECT 1 FROM T3
   WHERE T3.A = T1.A AND T3.B = T1.B
)

答案 1 :(得分:3)

select T1.A,T1.B,T1.C
from T1
inner join T2
on T1.X=T2.X
left join T3 on T1.A=T3.A and T1.B=T3.B
where T3.A is null

答案 2 :(得分:0)

你也可以使用像

这样的子查询来做到这一点
SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND T1.A NOT IN (SELECT T3.A FROM T3)
AND T2.B Not IN (SELECT T3.B FROM T3)

答案 3 :(得分:0)

SELECT T1.A, T1.B, T1.C
FROM T1, T2
WHERE T1.X=T2.X
AND (T1.A, T1.B) NOT IN (SELECT T3.A, T3.B   FROM T3 )