内部不存在Oracle不存在

时间:2014-02-27 11:55:52

标签: sql oracle not-exists

当我运行此查询时,伙计们:

SELECT * FROM big_table big
WHERE sum_number = 1
 AND NOT EXISTS ( SELECT 1
                  FROM smal_table smal
                  WHERE other_number = big.other_number
                )

我得到了4次重组,但是当我运行这个时

SELECT * FROM big_table big
WHERE sum_number = 1
 AND NOT EXISTS ( SELECT 1
                  FROM smal_table smal
                  WHERE other_number = big.other_number
                  AND NOT EXISTS ( SELECT 1
                                    FROM and_another_table another
                                    WHERE and_another_number = big.and_another_number
                                 )
                )

我从big_table获得所有行。 有没有人有任何想法为什么会这样? 我想做一个查询,向我展示big_table中不存在于smal_table中的所有行,但如果它存在于小表中,我只想要不在and_another_table中的行 有人可以帮帮我吗?

编辑:我想要big_table中不在small_table中的行加上big_table中的行,这些行在small_table上但不在and_another_table中

1 个答案:

答案 0 :(得分:0)

我认为逻辑是你希望大表中的行不在小表或另一个表中。如果是这样,这应该做你想要的:

SELECT *
FROM big_table big
WHERE sum_number = 1 AND
      (NOT EXISTS (SELECT 1
                   FROM small_table smal
                   WHERE other_number = big.other_number
                  ) OR
       NOT EXISTS (SELECT 1
                   FROM and_another_table another
                   WHERE and_another_number = big.and_another_number
                  )
      );

编辑:

根据以下评论,where子句将是:

WHERE sum_number = 1 AND
      (NOT EXISTS (SELECT 1
                   FROM small_table smal
                   WHERE other_number = big.other_number
                  ) OR
       EXISTS (SELECT 1
               FROM and_another_table another
               WHERE and_another_number = big.and_another_number
              )
      );