如何比较postgres中每列的两个相同表数据?

P S*_*S M 1 postgresql

我想比较两个表的所有列值。两个表是相同的表意味着列号相同且主键相同。任何人都可以建议在 postgres 中比较这两个表的查询吗?查询应该给出列名以及两个表的两个不同值是什么。就像这样

pkey | column_name | table1_value | table2_value
123  | bonus       |   1          |    0
Run Code Online (Sandbox Code Playgroud)

小智 6

要获取所有不同的行,您可以使用:

select *
from table_1 t1
  join table_2 t2 on t1.pkey = t2.pkey 
where t1 is distinct from t2;
Run Code Online (Sandbox Code Playgroud)

这只会比较两个表中都存在的行。如果您还想查找其中缺少的内容,请使用完整的外连接:

select coalesce(t1.pkey, t2.pkey) as pkey,
       case 
         when t1.pkey is null then 'Missing in table_1'
         when t2.pkey is null then 'Missing in table_2'
         else 'At least one column is different'
       end as status,
       *
from table_1 t1
  full ojoin table_2 t2 on t1.pkey = t2.pkey 
where (t1 is distinct from t2)
   or (t1.pkey is null)
   or (t2.pkey is null);
Run Code Online (Sandbox Code Playgroud)

如果安装hstore扩展,您可以以键/值映射的形式查看差异:

select coalesce(t1.pkey, t2.pkey) as pkey,
       case 
         when t1.pkey is null then 'Missing in table_1'
         when t2.pkey is null then 'Missing in table_2'
         else 'At least one column is different'
       end as status,
       hstore(t1) - hstore(t2) as values_in_table_1, 
       hstore(t2) - hstore(t1) as values_in_table_2
from table_1 t1
  full ojoin table_2 t2 on t1.pkey = t2.pkey 
where (t1 is distinct from t2)
   or (t1.pkey is null)
   or (t2.pkey is null);
Run Code Online (Sandbox Code Playgroud)

使用此示例数据:

create table table_1 (pkey integer primary key, col_1 text, col_2 int);
insert into table_1 (pkey, col_1, col_2) 
values (1, 'a', 1), (2, 'b', 2), (3, 'c', 3), (5, 'e', 42);

create table table_2 (pkey integer primary key, col_1 text, col_2 int);
insert into table_2 (pkey, col_1, col_2) 
values (1,'a', 1), (2, 'x', 2), (3, 'c', 33), (4, 'd', 52);
Run Code Online (Sandbox Code Playgroud)

可能的结果是:

pkey | status                           | values_in_table_1 | values_in_table_2
-----+----------------------------------+-------------------+------------------
   2 | At least one column is different | "col_1"=>"b"      | "col_1"=>"x"     
   3 | At least one column is different | "col_2"=>"3"      | "col_2"=>"33"    
   4 | Missing in table_1               |                   |                  
   5 | Missing in table_2               |                   |                  
Run Code Online (Sandbox Code Playgroud)