2012-02-21 56 views
0

我有一張表,其中包含舊的舊數據以及新數據。舊數據沒有像新數據那樣填充所有字段。我想更新舊的數據,以便它匹配新的東西。這些字段是整數並代表用戶ID。 Created_By字段應該默認爲User_ID--這意味着在傳統數據中,我們將created_by值設置爲與user_id相同。更新SQL:從一個列中取值並在另一箇中替換爲空

實例字段:

Created_By | User_ID 
NULL  | 1234 
NULL  | 2345 
NULL  | 1234 
NULL  | 6742 
1234  | 1234 
2345  | 1234 

所以我想只更新NULL值。但是,我不知道如何獲取每行的User_ID。

事情是這樣的:

update my_table 
set Created_By = ??? (the respective User_ID for that row) 
where Created_By is null 

所以更新應該是這樣的:

Created_By | User_ID 
1234  | 1234 
2345  | 2345 
1234  | 1234 
6742  | 6742 
1234  | 1234 
2345  | 1234 

回答

8
update my_table 
set Created_By = User_ID 
where Created_By is null 
相關問題