简体   繁体   English

在没有多个连接的情况下查找两个表中的差异

[英]Find differences in both tables without multiple joins

After comparing two data sets I'd like to extract information such as:比较两个数据集后,我想提取信息,例如:

  • Rows that are only present in table A仅存在于表 A 中的行
  • Rows that are only present in table B仅存在于表 B 中的行
  • Non-key value differences after join加入后的非键值差异

What's the preferred way to go about this?解决这个问题的首选方法是什么? Is there a way to do this without having to do LEFT and RIGHT joins separately?有没有办法做到这一点而不必分别进行 LEFT 和 RIGHT 连接?

I would typically use group by for this, so I'm not sure what the reference to multiple joins is.我通常会为此使用group by ,所以我不确定对多个连接的引用是什么。

select col1, col2, col3, sum(in_a) as a_cnt, sum(in_b) as b_cnt
from ((select col1, col2, col3, 1 as in_a, 0 as in_b
       from a
      ) union all
      (select col1, col2, col3, 0 as in_a, 1 as in_b
       from b
      )
     ) ab;

It sounds you want a FULL OUTER JOIN .听起来你想要一个FULL OUTER JOIN That gives you all rows from both tables, joining ones that match keys.这为您提供了两个表中的所有行,加入了匹配键的行。 Then you can see which rows are present in only one table, and compare values for rows that are in both.然后,您可以查看哪些行仅出现在一个表中,并比较两者中的行的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM