1
votes

I have one TABLE

TABLE A

COL 1     COL 2    COL 3    COl 4
fname     lname    phone    email

And I want to insert the name of firstname & lastname I mean name = fname + lname

My Query is

INSERT INTO `user_accounts` (`name`, `phone`, `email`)
SELECT  (`COL 1` +` COL 2`),  `COL 3` ,  `COL 4`  FROM  `TABLE A`

How can i insert with select statement? Can i use the concat function?.

3
You have to use CONCAT function instead of + to concat field - Sadikhasan

3 Answers

1
votes

You can use concat to combine values from multiple columns.

INSERT INTO `user_accounts` (`name`, `phone`, `email`)
SELECT  concat( `COL 1`, ' ', `COL 2` ),  `COL 3` ,  `COL 4`  FROM  `TABLE A`
1
votes

The idea is correct, but MySQL uses concat() for string concatenation:

INSERT INTO `user_accounts`(`name`, `phone`, `email`)
    SELECT  concat(`COL 1`, ` COL 2`),  `COL 3`,  `COL 4`
    FROM  `TABLE A`;
0
votes

Yes. We can combine using concat function like this.

INSERT INTO `table_name` (`name`, `phone`)
SELECT  concat( `COL 1`, ' ', `COL 2` ), concat( `COL 3` , ' ', `COL 4 `) FROM  `TABLE A`