2

I was wondering if you have two columns for lets say

first name. 
last name.

what you store in the database.

Can you create a 'dynamic' column 'fullname' in the database that automatically creates the name out of the first and last name?

firstname   |lastname   |fullname (this goes automatically)
-----------------------------------------
foo         |bar        |foo bar 
4
  • why do you want to store this in the database? You can easily create this via a select query. Commented Nov 25, 2012 at 17:20
  • i understand but then you keep writing select statements. Commented Nov 25, 2012 at 17:24
  • Then don't keep writing the same sql more than once. Wrap it in a function and call that function instead. Commented Nov 25, 2012 at 17:27
  • well then you need to do that for each join, different statements ect, you do and would require to go trough all the code again to change the sql with the function Commented Nov 25, 2012 at 17:47

3 Answers 3

8

If you just want to select the full name, you can do just that:

 select concat(firstname, ' ', lastname) as fullname from users

If you actually want a column (i.e. be able to perform updates against it), then it's not possible, AFAIK.

Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this:

 SELECT CONCAT(firstname, ' ', lastname) as firstlast FROM users

Comments

0

This one also makes sure that no empty spaces are added:

SELECT IF(ISNULL(firstname), lastname, CONCAT(firstname, ' ', lastname)) AS fullname FROM users

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.