您的位置:首页技术文章
文章详情页

mysql - sql 找出2个数据库的差异表名

【字号: 日期:2022-06-22 09:26:52浏览:17作者:猪猪

问题描述

同一个数据库,本地51张表和远程49张表,有差异数据表。如何通过一条SQL来快速找出这些表的名字。

SQL:

USE performance_schema;SELECT t1.OBJECT_SCHEMA,t1.OBJECT_NAME,t2.OBJECT_SCHEMA,t2.OBJECT_NAMEFROM `table_io_waits_summary_by_table` t1 RIGHT JOIN `table_io_waits_summary_by_table` t2 ON t1.OBJECT_NAME = t2.OBJECT_NAMEWHERE t1.OBJECT_SCHEMA=’db1_local’ AND t2.OBJECT_SCHEMA=’db2_remote’;

结果集只有49张,无法罗列出差异的表明。使用了 LEFT OUTER JOIN 还是一样。

验证是存在差异的:

SELECT OBJECT_NAMEFROM table_io_waits_summary_by_table WHERE OBJECT_SCHEMA=’db1_local’ AND OBJECT_NAME NOT IN (SELECT OBJECT_NAME FROM table_io_waits_summary_by_table WHERE OBJECT_SCHEMA=’db2_remote’ )

问题解答

回答1:

试试这个:

USE performance_schema;SELECT t1.*FROM `table_io_waits_summary_by_table` t1 LEFT JOIN `table_io_waits_summary_by_table` t2 ON t1.OBJECT_NAME = t2.OBJECT_NAME AND t2.OBJECT_SCHEMA=’db2_remote’WHERE t1.OBJECT_SCHEMA=’db1_local’ AND t2.OBJECT_NAME IS NULL;

其实你的第一个SQL只要将对t2的限制提到连接条件中就行了,将t2.OBJECT_SCHEMA=’db2_remote’写在where条件里面RIGHT JOIN就变成了INNER JOIN ~

相关文章: