該查詢會給你的結果:
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
並插入和更新在你的表3的結果:
INSERT INTO [Table 3](idhash, username, usermail, firstseen, lastseen)
SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash
LEFT JOIN [Table 3] C
ON A.idhash = C.idhash
WHERE C.idhash IS NULL
-- For SQL Server
UPDATE T3
SET firstseen = T1.firstseen,
lastseen = T1.lastseen
FROM [Table 3] T3
INNER JOIN (SELECT A.*, B.firstseen, B.lastseen
FROM [Table 1] A
LEFT JOIN [Table 2] B
ON A.idhash = B.idhash) T1
WHERE T3.firstseen != T1.firstseen OR T3.lastseen != T1.lastseen
你在使用什麼數據庫服務器? – 2012-02-16 15:08:06
爲了澄清,您說表2中有表1中沒有相應行的行?我在問,因爲很多答案都是相反的。 – 2012-02-16 15:51:22