比较两个相似表、不同数据库之间的数据
Compare data between two similar tables, different database
我们遇到了这个问题,有人对我们想要改回的数据库进行了一些更改。我们有 2 个数据库副本:几天前的备份和他们的生产数据库。
我正在尝试比较并获取 Statecode from db1 != Statecode from db2 的记录。我该怎么做呢?
我试过以下方法,但我认为这是错误的:
SELECT fileId, statecode from DB1..fileBase t1
WHERE EXISTS
(
SELECT fileId, statecode from DB2..fileBase t2
WHERE t1.statecode = 0 AND
t1.statecode != t2.statecode AND
t1.ModifiedBy = '326AC533-DD41-E411-213C-03156D015A13'
)
编辑示例:
Prod ID Backup ID ProdStateCode BackupStateCode
D4DEEA73-D5B6-E611-80E6-9216FEE69DCF D4DEEA73-D5B6-E611-80E6-9216FEE69DCF 0 1
621BD1FA-D1B6-E611-80E6-9216FEE69DCF 621BD1FA-D1B6-E611-80E6-9216FEE69DCF 0 0
2E4E65F0-D1B6-E611-80E6-9216FEE69DCF 2E4E65F0-D1B6-E611-80E6-9216FEE69DCF 0 0
35B8FCD9-D0B6-E611-80E6-9216FEE69DCF 35B8FCD9-D0B6-E611-80E6-9216FEE69DCF 0 1
我想获取那些 Prod StateCode 不等于 Backup State Code 的代码。我也试过了
SELECT
prod.fileId,
prod.statecode
FROM DB1..fileBase AS prod
INNER JOIN DB2..fileBase AS back
ON prod.fileId = back.fileId
WHERE prod.statecode != back.statecode
但输出似乎不正确。
试试这个: 使用 LEFT JOIN
或 NOT EXISTS
如下所示,它将为您提供 field & statecode
的列表,其中 field
在 table 中都相等,但 statecode
在 db2
table
中不存在
SELECT prod.fileId, prod.statecode
FROM DB1.fileBase AS prod
LEFT JOIN DB2.fileBase AS back ON prod.fileId = back.fileId
AND prod.statecode = back.statecode
WHERE back.statecode IS NULL
或
SELECT prod.fileId, prod.statecode
FROM DB1.fileBase AS prod
WHERE NOT EXISTS(
SELECT 1 FROM DB2.fileBase
WHERE prod.fileId = back.fileId--Only required if you wanna compare with statecode
AND prod.statecode = back.statecode)
我们遇到了这个问题,有人对我们想要改回的数据库进行了一些更改。我们有 2 个数据库副本:几天前的备份和他们的生产数据库。
我正在尝试比较并获取 Statecode from db1 != Statecode from db2 的记录。我该怎么做呢?
我试过以下方法,但我认为这是错误的:
SELECT fileId, statecode from DB1..fileBase t1
WHERE EXISTS
(
SELECT fileId, statecode from DB2..fileBase t2
WHERE t1.statecode = 0 AND
t1.statecode != t2.statecode AND
t1.ModifiedBy = '326AC533-DD41-E411-213C-03156D015A13'
)
编辑示例:
Prod ID Backup ID ProdStateCode BackupStateCode
D4DEEA73-D5B6-E611-80E6-9216FEE69DCF D4DEEA73-D5B6-E611-80E6-9216FEE69DCF 0 1
621BD1FA-D1B6-E611-80E6-9216FEE69DCF 621BD1FA-D1B6-E611-80E6-9216FEE69DCF 0 0
2E4E65F0-D1B6-E611-80E6-9216FEE69DCF 2E4E65F0-D1B6-E611-80E6-9216FEE69DCF 0 0
35B8FCD9-D0B6-E611-80E6-9216FEE69DCF 35B8FCD9-D0B6-E611-80E6-9216FEE69DCF 0 1
我想获取那些 Prod StateCode 不等于 Backup State Code 的代码。我也试过了
SELECT
prod.fileId,
prod.statecode
FROM DB1..fileBase AS prod
INNER JOIN DB2..fileBase AS back
ON prod.fileId = back.fileId
WHERE prod.statecode != back.statecode
但输出似乎不正确。
试试这个: 使用 LEFT JOIN
或 NOT EXISTS
如下所示,它将为您提供 field & statecode
的列表,其中 field
在 table 中都相等,但 statecode
在 db2
table
SELECT prod.fileId, prod.statecode
FROM DB1.fileBase AS prod
LEFT JOIN DB2.fileBase AS back ON prod.fileId = back.fileId
AND prod.statecode = back.statecode
WHERE back.statecode IS NULL
或
SELECT prod.fileId, prod.statecode
FROM DB1.fileBase AS prod
WHERE NOT EXISTS(
SELECT 1 FROM DB2.fileBase
WHERE prod.fileId = back.fileId--Only required if you wanna compare with statecode
AND prod.statecode = back.statecode)