Friday, January 22, 2021

SQL to replace username field with user id

The scenario is, a table called assets was loaded through a script, but the column updated_by which should contain the user id, is stored with the user name instead. How to replace the updated_by field with the user id?


Here is an example of the table structure;

assets(id, name, description, updated_by)

users(id, name)


Solution 

Through the SQL command;

UPDATE assets t2

SET    updated_by = t1.id

FROM   users t1

WHERE  t2.updated_by = t1.user_id;

No comments:

Blog Archive