简体   繁体   中英

Update SQL: Take Value from One Column and Replace Null in Another

I have a table that has old, legacy data along with new data. The old data does not have all the fields populated like the new data. I want to update the old data so that it matches the new stuff. The fields are ints and represent user IDs. The Created_By field should default to the User_ID -- meaning that in the legacy data, we're setting the created_by value to be the same as the user_id.

Example fields:

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

So I want to only update the NULL values. However, I do not know how to grab the User_ID for each row.

Something like this:

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

So the update should look like this:

Created_By | User_ID
1234       | 1234
2345       | 2345
1234       | 1234
6742       | 6742
1234       | 1234
2345       | 1234
update my_table
set Created_By = User_ID
where Created_By is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM