我需要比较不同数据库中类似表之间的行值。我如何知道哪些行不同。
e.g。 db1.foo (contains 2000 rows) and db2.foo (contains 2003 rows)
。这两个表应该是相似的,我想知道如何找到应该是添加的行。注意:即使这些表中的主键ID也应该是相同的。
我无法访问任何GUI工具,并且想知道是否有任何SQL命令可用于执行此差异?
答案 0 :(得分:0)
仅取决于对主键的检查:
SELECT
'a' AS DataOnlyInTable
, a.*
FROM
db1.foo AS a
LEFT JOIN
db2.foo AS b
ON b.PK = a.PK
WHERE b.PK IS NULL
UNION ALL
SELECT
'b' AS DataOnlyInTable
, b.*
FROM
db1.foo AS a
RIGHT JOIN
db2.foo AS b
ON b.PK = a.PK
WHERE a.PK IS NULL
根据对整行的检查:
SELECT
'a' AS DataOnlyInTable
, a.*
FROM
db1.foo AS a
LEFT JOIN
db2.foo AS b
ON (b.PK, b.column2, b.column3, ...)
= (a.PK, a.column2, a.column3, ...)
WHERE b.PK IS NULL
UNION ALL
SELECT
'b' AS DataOnlyInTable
, b.*
FROM
db1.foo AS a
RIGHT JOIN
db2.foo AS b
ON (b.PK, b.column2, b.column3, ...)
= (a.PK, a.column2, a.column3, ...)
WHERE a.PK IS NULL