我有两个相同的表,大部分数据都是相同的
Table 1
-------------------------------
C1 C2 C3 C4 C5
R1 1 2 3 4 5
R2 6 7 8 9 10
Table 2
-------------------------------
C1 C2 C3 C4 C5
R1 1 2 3 4 5
R2 6 17 18 9 10
R3 11 12 13 14 15
R4 16 17 18 19 20
有人可以帮我编写查询以比较两个表,在结果集中获取R3,R4,还可以获取列值不同的行,例如C2R2,C3R2
答案 0 :(得分:0)
假设第一列(值为R1
,R2
等)被命名为row
(您不用说):
select row, 'Not present in table 2' as difference
from table_1
where row not in (select row from table_2)
union all
select row, 'Not present in table 1'
from table_2
where row not in (select row from table_1)
union all
select
t1.row,
concat(
case when t1.c1 <> t2.c1 then 'c1 ' end,
case when t1.c2 <> t2.c2 then 'c2 ' end,
case when t1.c3 <> t2.c3 then 'c3 ' end,
case when t1.c4 <> t2.c4 then 'c4 ' end,
case when t1.c5 <> t2.c5 then 'c5 ' end
)
from table_1 t1
join table_2 t2 on t1.row = t2.row
where t1.c1 <> t2.c1
or t1.c2 <> t2.c2
or t1.c3 <> t2.c3
or t1.c4 <> t2.c4
or t1.c5 <> t2.c5