沒有年齡,姓名,職業構成主鍵(或至少一個唯一的鑰匙?):
如果是這樣,你可以這樣做:
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