sql - 需要找出表之间的差异

标签 sql database rdbms

我的表结构如下: enter image description here

我需要找到表之间的差异,因为哪些数据在另一个表中不可用(反之亦然)。 我可以找到不同之处如下:

enter image description here

使用的 SQL 查询:

select * 
from (select input_name_id, count(1) as cnt
      from Table1
      group by input_name_id
     ) a join 
     (select input_name_id, count(1) as cnt
      from Table2
      group by input_name_id
     ) b
     on (a.input_name_id = b.input_name_id)
where a.cnt <> b.cnt

预期结果:

enter image description here

我尝试了多种方法来提取数据,但我做不到! 非常感谢您的帮助。谢谢

最佳答案

两件事:(1)全外连接; (2) 枚举具有相同值的行:

select * 
from (select input_name_id, match_id, name, 
             row_number() over (partition by input_name_id, match_id, name order by name) as seqnum
      from Table1
     ) a full join 
     (select input_name_id, match_id, name, 
             row_number() over (partition by input_name_id, match_id, name order by name) as seqnum
      from Table2
     ) b
     on a.input_name_id = b.input_name_id and
        a.match_id = b.match_id and
        a.name = b.name and
        a.seqnum = b.seqnum
where a.seqnum is null or b.seqnum is null;

关于sql - 需要找出表之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42392044/

相关文章:

performance - ORM 查询性能与 RDBMS 性能

sql-server - 使用 'where then Union' 或使用 'Union then Where'

mysql - 如何在一个触发器中放置多组方程式

PHP SQL 列 = 列减去 $value

mysql - 如何只将一个表中的一列插入到多列表中?

java - 使用 jdbc 从 sql server 获取成功批量插入的行数

database - 使用 Visual Studio 2010 Access 2010 数据库

mysql - 将 Laravel 应用程序连接到另一个数据库 - Laravel 5.8

sql - MongoDB : few questions

mysql - 如何在 sql 中返回 group_concat 的计数?