[英]SQL | How to compare two tables with same structure
我有兩個結構相同的表,T_1 = [A, B, C] T_2 = [A, B, C]。 他們之間在價值觀上存在差異。 如何使用查詢獲取這些值?
問候
您可以使用group by
和聚合來獲取一個表中而不是另一個表中的值:
select sum(in_1), sum(in_2), a, b, c
from ((select 1 as in_1, 0 as in_2, a, b, c
from t_1
) union all
(select 0, 1, a, b, c
from t_2
)
) tt
group by a, b, c
having sum(in_1) <> sum(in_2);
請注意,這是非常通用的。 它處理列中的NULL
值。 它還處理表中的重復項,確保計數相同。
您可以嘗試一種簡單的編碼方式,但性能不夠好
(select * from T_1
minus
select * from T_2)
union all
(
select * from T_2
minus
select * from T_1
) ;
如果您正在尋找另一種方式,我建議您查看此http://www.dba-oracle.com/t_convert_set_to_join_sql_parameter.htm
SELECT * FROM TABLE1 EXCEPT SELECT * FROM TABLE2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.