Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating column with value from another column in postgres table

I have a postgres table which has 2 columns

username and email I have hundreds of rows in the table such as

username | email
username1 | [email protected]
username2 | [email protected]
username3 | [email protected]
username4 | [email protected]

The way this was setup all emails are the same, and now I need to make them unique. I am trying to update the email column to be like this

username | email
username1 | [email protected]
username2 | [email protected]
username3 | [email protected]
username4 | [email protected]

basically copy over the value from the username column and add it to the email column. I tried using the coalesce function but that will replace the value completely and not update it.

Please can you help me understand how to achieve this.

Thanks

like image 970
Vik G Avatar asked Dec 13 '25 06:12

Vik G


1 Answers

An update with basic concatenation should work here:

UPDATE yourTable
SET email = username || '_' || email;
like image 121
Tim Biegeleisen Avatar answered Dec 15 '25 20:12

Tim Biegeleisen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!