我们遇到了这个问题,有人对数据库进行了一些更改,我们想改回去。我们有数据库的 2 个副本:几天前的备份和他们的生产数据库。
我正在尝试比较并获取来自 db1 的状态代码 != 来自 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 状态代码不等于备份状态代码的人。我也试过
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
相等,但db2
表中不存在statecode
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)