首页 > 解决方案 > Oracle 为什么存在查询返回此结果

问题描述

我很困惑使用存在来比较两个表之间的两个数据。我期望从查询中获得 0 行,但它返回一些数据。我不知道为什么。

select main_rule, child_rule from scoredef_detail s where s.main_rule = 515;
1   515 516
2   515 517

select main_rule, child_rule from scoredef_detail_at s where s.main_rule = 515;
1   515 516
2   515 517

此查询返回上面的所有内容。我试图找出差异。这些列是其他一些表的外键。它可以与其他表的记录相关吗?

select main_rule, child_rule
  from scoredef_detail s
 where exists
 (select main_rule, child_rule
          from scoredef_detail_at sa
         where s.main_rule = sa.main_rule and (s.child_rule <> sa.child_rule));

提前致谢。

标签: sqloracle

解决方案


您可以使用 afull join来获取不匹配:

select s.*, sa.*
from (select main_rule, child_rule
      from scoredef_detail s
      where s.main_rule = 515
     ) s full join
     (select main_rule, child_rule
      from scoredef_detail_at s
      where s.main_rule = 515
     ) sa
     on sa.main_rule = s.main_rule and sa.child_rule = s.child_rule
where sa.main_rule is null or s.main_rule is null;

这将显示在一个表中但不在另一个表中的对。


推荐阅读