更新 SQL:从一列中获取值并替换另一列中的 Null
我有一个表,其中包含旧的遗留数据和新数据。旧数据没有像新数据那样填充所有字段。我想更新旧数据,使其与新数据匹配。这些字段是整数,代表用户 ID。 Created_By 字段应默认为 User_ID——这意味着在旧数据中,我们将created_by 值设置为与 user_id 相同。
示例字段:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)