What's the sql statement to insert firstname column and lastname column into fullname column

How do you create a default value of a column that is the concatenated value of two other columns? For example, I have a column for last_name and a column for first_name, and want to create a column for the full_name.
dynorichAsked:
Who is Participating?
 
Dan CraciunConnect With a Mentor IT ConsultantCommented:
BTW, if you already have the data in your table and just want to add the full_name column data, you can do it like this:

UPDATE table SET full_name = CONCAT(first_name, " ", last_name)

Open in new window

0
 
Dan CraciunIT ConsultantCommented:
AFAIK, you cannot use expressions as default values. Only constants and timestamp.

You can use a trigger BEFORE INSERT and one BEFORE UPDATE to obtain about the same effect.

HTH,
Dan
0
 
dynorichAuthor Commented:
Perfect, thank you
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Dan CraciunIT ConsultantCommented:
Glad I could help!
0
 
dynorichAuthor Commented:
Worked fine when I used it on SQL in database.
However when I added it to php page:
Line174  $query = "UPDATE customers SET ";
Line175       $query .= "Fullname = CONCAT{Firstname, " ", Lastname}";
Line176       $query .= " WHERE cid = {$TempID}";
I get the following error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home3/dynorich/public_html/extremeleverage/afu/noc/purchase_member.php on line 175.

Any ideas?
0
 
Dan CraciunIT ConsultantCommented:
Why the {}?
$query = "UPDATE customers SET ";
$query .= "Fullname = CONCAT(Firstname, " ", Lastname)";
$query .= " WHERE cid = $TempID";
0
 
dynorichAuthor Commented:
I got the error using the() brackets so I was trying the {} brackets. I've changed it back to:
$query = "UPDATE customers SET ";
       $query .= "Fullname = CONCAT(Firstname, " ", Lastname)";
       $query .= " WHERE cid = $TempID";
but still get same error
Would it have anything to do with the " " between Firstname and Lastname
0
 
Dan CraciunIT ConsultantCommented:
Yup.
$query .= "Fullname = CONCAT(Firstname, ' ', Lastname)";
0
 
dynorichAuthor Commented:
That worked. Thank you sir.
0
 
Dan CraciunIT ConsultantCommented:
You're welcome!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.