mysql - sql 找出2个数据库的差异表名
问题描述
同一个数据库,本地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 ~
相关文章:
1. docker不显示端口映射呢?2. golang - 用IDE看docker源码时的小问题3. nignx - docker内nginx 80端口被占用4. docker - 如何修改运行中容器的配置5. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?6. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下7. 在windows下安装docker Toolbox 启动Docker Quickstart Terminal 失败!8. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””9. 网页爬虫 - python爬虫翻页问题,请问各位大神我这段代码怎样翻页,还有价格要登陆后才能看到,应该怎么解决10. python在list相加 求助