没有年龄,姓名,职业构成主键(或至少一个唯一的钥匙?):
如果是这样,你可以这样做:
SELECT
t1.name, t1.age, t1.profession,
t1.column1 as t1column1, t2.column1 as t2column1,
t1.column2 as t1column2, t2.column2 as t2column2,
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
(
t1.column1<>t2.column1
OR t1.column2<>t2.column2
OR t1.column3<>t2.column3)
当然,这需要一个在两个表中都相同的唯一键。另外,我清楚地更改了查询的结果,以显示所有列,而不是仅显示已更改的列。在单个T-SQL查询中识别出那样的变化是尴尬的(但可能),所以我的建议是像这样返回它,根据您的使用情况,应用程序/表示层可以处理查找哪列更改或只是扫描它的眼睛。
如果你真的想这样做在T-SQL,您可以与工会做到这一点,如:
SELECT
t1.name, t1.age, t1.profession,
'column1' as ColumnChanged,
t1.column1 as oldValue,
t2.column1 as newValue
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
t1.column1<>t2.column1
UNION ALL #better peformance than UNION, which would enforce uniqueness
SELECT
t1.name, t1.age, t1.profession,
'column2' as ColumnChanged,
t1.column2 as oldValue,
t2.column2 as newValue
FROM
table1 t1, table2 t2
WHERE
(t1.name = t2.name and t1.age = t2.age and t1.profession = t2.profession) and
t1.column2<>t2.column2
.......
如何table1得到更新?请提供一些测试数据/方案以帮助我们理解。 – ganders
我不认为这是相关的。如果我错了,请解释原因。 –
是的,我重新读你的问题。见下面的答案。 – ganders