Mysql 比较2个类似表中的2行,只返回具有不同值的列名

Mysql 比较2个类似表中的2行,只返回具有不同值的列名,mysql,sql,database,join,Mysql,Sql,Database,Join,我有两个表TblA和TblBTblA有ABC…Z列和TblB也有ABCD…Z列列。我希望列的名称在TblA和TblB数据不同的特定行中有所不同。假设列A是主键并且从不更改,即可以对列A执行join,遗憾的是,在将版本/默认值/历史记录表与另一个表进行比较时,没有比逐列查询更好的方法了 select case when a.B!=b.B then 'B' else null end, case when a.C!=b.C then 'C' else null end, ....(repeat f

我有两个表
TblA
TblB
TblA
ABC…Z列
TblB
也有
ABCD…Z列
列。我希望列的名称在
TblA
TblB
数据不同的特定行中有所不同。假设
列A
主键
并且从不更改,即可以对
列A
执行
join
,遗憾的是,在将版本/默认值/历史记录表与另一个表进行比较时,没有比逐列查询更好的方法了

select 
case when a.B!=b.B then 'B' else null end,
case when a.C!=b.C then 'C' else null end,
....(repeat for each column)

from tbla a
left join tblb b
on a.A=b.A

请记住,如果列可以包含null,
null=任何内容都是null(不是true或false),那么您可能需要在ifnull()中包装每一列以进行比较

遗憾的是,在将一个版本/默认值/历史记录表与另一个表进行比较时,没有比逐列查询更好的方法了

select 
case when a.B!=b.B then 'B' else null end,
case when a.C!=b.C then 'C' else null end,
....(repeat for each column)

from tbla a
left join tblb b
on a.A=b.A

请记住,如果列可以包含null,
null=任何内容都是null(不是true或false),那么您可能需要在ifnull()中包装每一列以进行比较

遗憾的是,在将一个版本/默认值/历史记录表与另一个表进行比较时,没有比逐列查询更好的方法了

select 
case when a.B!=b.B then 'B' else null end,
case when a.C!=b.C then 'C' else null end,
....(repeat for each column)

from tbla a
left join tblb b
on a.A=b.A

请记住,如果列可以包含null,
null=任何内容都是null(不是true或false),那么您可能需要在ifnull()中包装每一列以进行比较

遗憾的是,在将一个版本/默认值/历史记录表与另一个表进行比较时,没有比逐列查询更好的方法了

select 
case when a.B!=b.B then 'B' else null end,
case when a.C!=b.C then 'C' else null end,
....(repeat for each column)

from tbla a
left join tblb b
on a.A=b.A

请记住,如果列可以包含null,
null=任何内容都是null(不是true或false),那么您可能需要将每个列包装在ifnull()中以进行比较

编辑:我在这里没有处理null。您可以使用数据库的函数来处理这些问题

这是3列的示例查询。您可以将其扩展到其他列

          with s1 (A    ,B  ,C) as
          (select 1,22,23 from dual union
           select  2,45,47 from dual union
           select  3,66,    68 from dual
          ),
          t1 (A ,B  ,C) as
          (select 1,23,24 from dual union
           select  2,45,47 from dual union
           select  3,66,    69 from dual
          ),
          chng as(
          select s1.*, case when s1.B = t1.B then '' else 'B'  end as B1 , case when s1.C = t1.C then '' else 'C'  end as C1
          from s1 ,t1
          where s1.A = t1.A
          )
           ,chkChange as(
          select a, (B1||C1) as Changes from chng
          )
          select * from chkChange
          where changes is not null

编辑:我没有在这里处理空值。您可以使用数据库的函数来处理这些问题

这是3列的示例查询。您可以将其扩展到其他列

          with s1 (A    ,B  ,C) as
          (select 1,22,23 from dual union
           select  2,45,47 from dual union
           select  3,66,    68 from dual
          ),
          t1 (A ,B  ,C) as
          (select 1,23,24 from dual union
           select  2,45,47 from dual union
           select  3,66,    69 from dual
          ),
          chng as(
          select s1.*, case when s1.B = t1.B then '' else 'B'  end as B1 , case when s1.C = t1.C then '' else 'C'  end as C1
          from s1 ,t1
          where s1.A = t1.A
          )
           ,chkChange as(
          select a, (B1||C1) as Changes from chng
          )
          select * from chkChange
          where changes is not null

编辑:我没有在这里处理空值。您可以使用数据库的函数来处理这些问题

这是3列的示例查询。您可以将其扩展到其他列

          with s1 (A    ,B  ,C) as
          (select 1,22,23 from dual union
           select  2,45,47 from dual union
           select  3,66,    68 from dual
          ),
          t1 (A ,B  ,C) as
          (select 1,23,24 from dual union
           select  2,45,47 from dual union
           select  3,66,    69 from dual
          ),
          chng as(
          select s1.*, case when s1.B = t1.B then '' else 'B'  end as B1 , case when s1.C = t1.C then '' else 'C'  end as C1
          from s1 ,t1
          where s1.A = t1.A
          )
           ,chkChange as(
          select a, (B1||C1) as Changes from chng
          )
          select * from chkChange
          where changes is not null

编辑:我没有在这里处理空值。您可以使用数据库的函数来处理这些问题

这是3列的示例查询。您可以将其扩展到其他列

          with s1 (A    ,B  ,C) as
          (select 1,22,23 from dual union
           select  2,45,47 from dual union
           select  3,66,    68 from dual
          ),
          t1 (A ,B  ,C) as
          (select 1,23,24 from dual union
           select  2,45,47 from dual union
           select  3,66,    69 from dual
          ),
          chng as(
          select s1.*, case when s1.B = t1.B then '' else 'B'  end as B1 , case when s1.C = t1.C then '' else 'C'  end as C1
          from s1 ,t1
          where s1.A = t1.A
          )
           ,chkChange as(
          select a, (B1||C1) as Changes from chng
          )
          select * from chkChange
          where changes is not null

但是为什么有两个表具有相同的布局和几乎相同的数据?因为一个表存储更新的数据,而另一个表存储其以前的值。列数真的有那么高(20+)?你能发布结果应该是什么样的吗?你真的只想要列名吗?但为什么你有两个布局相同、数据几乎相同的表?因为一个表存储更新的数据,另一个表存储以前的值。列数真的有那么多(20+)?你能发布结果应该是什么样的吗?你真的只想要列名吗?但为什么你有两个布局相同、数据几乎相同的表?因为一个表存储更新的数据,另一个表存储以前的值。列数真的有那么多(20+)?你能发布结果应该是什么样的吗?你真的只想要列名吗?但为什么你有两个布局相同、数据几乎相同的表?因为一个表存储更新的数据,另一个表存储以前的值。列数真的有那么多(20+)?你能发布结果应该是什么样的吗?您真的只需要列名吗?Mysql不支持WITH。Mysql不支持WITH。Mysql不支持WITH。Mysql不支持WITH。