PostgreSQL - update table using select statement

时间:2016-04-04 18:06:49

标签: sql postgresql

My question was to take the first letter of a survey responders first name and the first letter of the responders last name and concatenate the two so I get something along the lines of this: Sam Smith's would result in SS.

I'm using this query to do that and get the result I needed:

select concat(letterfirst, letterlast) as users
from (select substring(first_name,1,1) as letterfirst, 
             substring(last_name,1,1) as letterlast
       from survey_responders) as user1

Now I need to pass that result into the username column in the same table, survey_responders. I know I have to use a update statement but I can't seem to wrap my head around using a select statement with an update.

1 个答案:

答案 0 :(得分:3)

You are overcomplicating the situation:

update survey_responders
    set username = concat(left(first_name, 1), left(last_name, 1));
相关问题