您可以创建一个联合查询,例如:
SELECT Table2.Key1, Table2.Key2, Table2.Key3, CASE WHEN Table1.Value1 <> Table2.Value1 THEN 'Value1' END AS DifferingFields
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value1 <> Table2.Value1
联合所有
SELECT Table2.Key1, Table2.Key2, Table2.Key3, CASE WHEN Table1.Value2 <> Table2.Value2 THEN 'Value2' END
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value2 <> Table2.Value2
联合所有
SELECT Table2.Key1, Table2.Key2, Table2.Key3, CASE WHEN Table1.Value3 <> Table2.Value3 THEN 'Value3' END
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value3 <> Table2.Value3
[...为剩余的值字段添加语句...]
这将返回 3 个关键字段的值,其中包含不同值的字段名称。
对于 Microsoft Access,请使用:
SELECT Table2.Key1, Table2.Key2, Table2.Key3, IIF(Table1.Value1 <> Table2.Value1,"Value1","") AS DifferingFields
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value1 <> Table2.Value1
联合所有
选择 Table2.Key1,Table2.Key2,Table2.Key3,IIF(Table1.Value2 <> Table2.Value2,“Value2”,“”)
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value2 <> Table2.Value2
联合所有
选择 Table2.Key1,Table2.Key2,Table2.Key3,IIF(Table1.Value3 <> Table2.Value3,“Value3”,“”)
FROM Table2 LEFT JOIN Table1 ON Table2.Key1 = Table1.Key1 AND Table2.Key2 = Table1.Key2 AND Table2.Key3 = Table1.Key3
在哪里 Table1.Value3 <> Table2.Value3
如果考虑以下数据:
表 1:
Key1 Key2 Key3 Value1 Value2 Value3
1 2 3 4 5 6
1 2 4 4 5 8
1 2 5 4 5 10
1 2 6 4 6 10
表2:
键1 键 2 键 3 值 1 值 2 值
3 1 2 3 4 5 7
1 2 4 4 5 9
1 2 5 4 5 11
1 2 6 4 5 11
结果如下:
Key1 Key2 Key3 DifferingFields
1 2 6 Value2
1 2 3 Value3
1 2 4 Value3
1 2 5 Value3
1 2 6 Value3