skip to Main Content

Can anyone please help me with an SQL query which will allow me to populate the real_name column in table 1 with values from both first_name and second_name columns in table 2 by referencing the usernames.

TABLE 1

audit_username real_name
AHolland
THardy
LGentle
BGreen

TABLE 2

second_name first_name username
Holland Andrew AHolland
Hardy Tom THardy
Gentle Lauren LGentle
Green Barry BGreen

2

Answers


  1. How about

    UPDATE TABLE1 T1 SET 
    real_name=((SELECT CONCAT(firstname, ' ',  lastname) FROM TABLE2 WHERE username = T1.audit_username ))
    
    Login or Signup to reply.
  2. You can use an UPDATE statement with a JOIN to achieve this. Here’s an example of how you can do it:

    UPDATE table1 
    SET real_name = CONCAT(table2.first_name, ' ', table2.second_name)
    FROM table1
    INNER JOIN table2 ON table1.audit_username = table2.username;
    

    This SQL query will join table1 and table2 on the username field, and then update the real_name field in table1 with the first_name and second_name fields from table2, separated by a space.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search