如何比较2个mysql数据库数据

如何比较2个mysql数据库数据,第1张

如何比较2个mysql数据库数据

sql语句:因为MySQL 没有full outer join,所以用left join union all right join来实现

select a.table_schema,

a.table_name,

a.column_name,

b.table_schema,

b.table_name,

b.column_name

from information_schema.columns a

left join information_schema.columns b on a.table_name = b.table_name and a.column_name =

b.column_name andb.table_schema = 'DBNAMe1'

where a.table_schema = 'DBNAME2' and

(b.table_name is null or

b.column_name is null)

union all

select a.table_schema,

a.table_name,

a.column_name,

b.table_schema,

b.table_name,

b.column_name

from information_schema.columns a

left join information_schema.columns b on a.table_name = b.table_name and a.column_name =

b.column_name andb.table_schema = 'DBNAME2'

where a.table_schema = 'DBNAME1' and

(b.table_name is null or

b.column_name is NULL)

MySQL可以使用CASE语句来比较行中的两个数据。根据查询相关公开信息显示:从表中选择*,其中当col1>col2然后col1ELSEcol2结束的情况下;这样就可以比较表中每行col1和col2的值,并返回较大的那一个。

不建议图省事,老老实实写三条比较好。

insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.A = T2.A and ( T1.B = T2.B or T1.C = T2.C )

insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.B = T2.B and ( T1.A = T2.A or T1.C = T2.C )

insert into T2 (A,B,C) select T1.A,T1.B,T1.C from T1 left join T2 on T1.C = T2.C and ( T1.B = T2.B or T1.A = T2.A )


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7576192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存