2012-02-21 84 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 
相关问题