Lindsay
Lindsay

Reputation: 13

How do I combine data from 2 columns into 1 column in mysql and have it saved?

I am in the process of creating a new table of mock user id's and looking to combine first and last names into the new column UserIDs. I am successful in using the CONCAT string to combine the values; however, I have been unsuccessful in combining that with an INSERT INTO statement.

This is what I am attempting:

INSERT INTO person_userid
CONCAT(FirstName, LastName) as UserID 
from person_UserID.

Upvotes: 0

Views: 54

Answers (3)

jarlh
jarlh

Reputation: 44746

You want UPDATE, not INSERT:

UPDATE person_UserID SET UserID = CONCAT(FirstName, LastName) 

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133360

If you want use an insert select you should use this way

 INSERT INTO person_userid (user_id)
 select CONCAT(FirstName, LastName) 
 from person_UserID

but if the rows already exists could be you need a simple update ..

 update person_userid 
 set user_id = CONCAT(FirstName, LastName) 

Upvotes: 1

flip
flip

Reputation: 555

Your syntax for the INSERT statement is incorrect. Here is the INSERT reference.

INSERT INTO person_UserID (person_userid) VALUES (CONCAT(FirstName, LastName));

Upvotes: 0

Related Questions