如何在sql中自定义比较2个表的结果 [英] how to customize the result of comparing 2 tables in sql

查看:62
本文介绍了如何在sql中自定义比较2个表的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有2个表Table1和Table2,它们具有相同的列格式,例如,它们都具有以下列:

Hi all,I have 2 tables Table1 and Table2 which have the same format of column for example both of them have following columns:

A  B  C  D  E  F  G  ...


其中ID是主键.
现在如何编写一个SQL查询以逐列比较它们的数据,并将结果作为数据表返回,该数据表在表2的同一列旁边显示表1的每一列? 我在搜索中发现了一些方法,例如使用


where ID is the primary key.
Now how can i write a sql query to compare the data of them column by column and return the result as a datatable which shows each column of Table1 next to the same column of Table2?
I have found some ways in my searches such as using

minus ,Union All or Except

,但我的问题是结果中的列顺序.
注意:两个表中的列数均超过130.

我的意思是我想要这样的结果:

but my problem is sequence of columns in result.
Note : The number of columns in both tables is more than 130.

Edit : I mean i want something like this as the result:

A1  A2     B1  B2     C1  C2   ...

推荐答案

创建两个视图并将数据转储到其中.确保它们具有相同的列结构.
然后像现在一样使用UNION EXCEPT 比较这两个视图.
Create two views and the dump the data in them. Make sure they have the same column structure.
Then compare the two views using UNION or EXCEPT just like you have done now.


尝试一下,
try this,
select * from
(
    select * from tbl1
    except
    select * from tbl2
) as a
   
union all

select * from
(
    select * from tbl2
    except
    select * from tbl1
) as a


祝您编码愉快!
:)


Happy Coding!
:)


这篇关于如何在sql中自定义比较2个表的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆