MYSQL - select difference of rows counts in two tables

泪湿孤枕 提交于 2019-12-04 06:20:59

问题


I am trying to compare number of rows of two tables in two databases. The number of rows should be the same :

SELECT

(SELECT COUNT(*) FROM db1.table1)-

(SELECT COUNT(*) FROM db2.table1) AS difference

How do i select only if difference<>0? I need to run this for multiple tables and i don't need 0 values. I could load results in C# list and sort it out but i'd like to finish all in query.

I've tried using information_schema.TABLES for this but it's not suitable because it returns approximate results. I'm using Mysql 5.5


回答1:


SELECT * FROM (SELECT (SELECT COUNT() FROM db1.table1) - (SELECT COUNT() FROM db2.table1) AS t, "table1"AS table) X WHERE x.t1<>0 UNION ALL SELECT * FROM (SELECT (SELECT COUNT() FROM db1.table2) - (SELECT COUNT() FROM db2.table2) AS t, "table2"AS table) X WHERE x.t<>0



来源:https://stackoverflow.com/questions/45368388/mysql-select-difference-of-rows-counts-in-two-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!