2016-10-20 140 views
-1

我表中有許多重複的記錄同步,高度simplfied例子是:更新查詢重複記錄

name, emailaddress, importantid 
John Smith, [email protected], NULL 
John Smith, [email protected], 12345 
John Smith, [email protected], NULL 

在問題出現後,當另一臺連接到這一點,它可以連接到一個沒有importandd我需要的記錄。

我正在尋找更新表,以便對於每個電子郵件地址,它找到第一個importantid不爲空,然後使用該ID更新其他記錄,因此所有重複的帳戶最終有重要的ID。

我該怎麼做?

感謝

回答

1

SQL Fiddle Demo

UPDATE a 
SET a.importantid = b.importantid 
FROM test AS a 
JOIN (SELECT emailaddress, max(importantid) as importantid 
     FROM test 
     GROUP BY emailaddress) AS b 
ON a.emailaddress = b.emailaddress; 
+0

我必須仔細檢查其是否更新sintaxis。 –

0
UPDATE yourTable t1 
SET importanid =(SELECT max(importantid) 
      FROM yourTable t2 
      WHERE t2.emailaddress = t1.emailaddress AND t1.importantid is null and t2.importantid is not null)