急!急!急!求大神!MySQL对比2个数据库所有表以及表中的记录数

qq_17249217 2017-11-15 11:11:01
求大神!如何实现mysql 2个数据库所有表以及表中的记录数的对比,比方说一个数据库叫kafka,下面有很多表apply_info,cust_info..................另一个数据库叫ods,下面的表kafka_apply_info,kafka_cust_info...............如何实现这2个数据库中对应表的记录数是否相同,新手上路,求大神告知具体方法
...全文
169 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2017-11-16
  • 打赏
  • 举报
回复
set group_concat_max_len = 1024*10224 * 1024;
set @db1='kafka', @db2='ods';
select group_concat(concat(
	'select ', quote(a.table_name), 'as name',
	', (select count(*) from ', a.table_schema, '.', a.table_name, ') as ', a.table_schema,
	', (select count(*) from ', b.table_schema, '.', b.table_name, ') as ', b.table_schema
	) separator ' union all ')
into @sql
from information_schema.tables a, information_schema.tables b
where a.table_name=b.table_name
	and a.table_schema=@db1 and b.table_schema=@db2
;
set @sql=concat('select *, if(', @db1, '=', @db2, ', 1, 0) as eq from(', @sql, ') data');
prepare st from @sql;
execute st;
deallocate prepare st;



zjcxc 2017-11-16
  • 打赏
  • 举报
回复
对于 innodb, information_schema 中的记录数是不准确的 标准的做法还是做 count(*)
qq_17249217 2017-11-15
  • 打赏
  • 举报
回复
跪求大神讲授
ZHOU西口 2017-11-15
  • 打赏
  • 举报
回复
1、如果两个库在一个server上, select * from information_schema where table_schema='库名',查询比较
  • 打赏
  • 举报
回复
你的两个数据库是在同一个msyql上?
kampoo 2017-11-15
  • 打赏
  • 举报
回复
补充t2.table_rows,这样可以比较两个表的行数,当然也可以加到SQL语句里:
引用 2 楼 kampoo 的回复:
SQL类似,如果需要查询更多列注意看tables的表结构: select t1.table_name, t1.table_rows, t2.table_name, t2.table_rows from (select * from information_schema.tables where table_schema = 'kafka') t1 left join (select * from information_schema.tables where table_schema = 'ods') t2 on (t1.table_name = t2.table_name); 结果类似下面的列表,第二个表名列为空表示 在第二个数据库里没有这个表: t1.table_name table_rows t2.table_name bw_meter 199 bw_meter 5 bw_realtime_data 15027759 bw_realtime_data 455481 bw_config 12 ...
kampoo 2017-11-15
  • 打赏
  • 举报
回复
SQL类似,如果需要查询更多列注意看tables的表结构: select t1.table_name, t1.table_rows, t2.table_name from (select * from information_schema.tables where table_schema = 'kafka') t1 left join (select * from information_schema.tables where table_schema = 'ods') t2 on (t1.table_name = t2.table_name); 结果类似下面的列表,第二个表名列为空表示 在第二个数据库里没有这个表: t1.table_name table_rows t2.table_name bw_realtime_data 15027759 bw_realtime_data bw_meter 199 bw_meter bw_config 12 ...

56,673

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧