Search code examples
mysqlconcatenation

How to add a concatenated column to the table in MySQL?


I want to take my existing columns First_Name and Last_Name and concatenate them to add a new column Full_Name to the table Employees. (I am working on MySQL Workbench.)

So, I tried concatenating columns of First_Name and Last_Name using the following query:

SELECT CONCAT(First_Name, " ", Last_Name) AS Full_Name FROM Employees;

But this is only for display purpose so if I wanted to add this column into the table, how do I do it??

This query is also for displaying purpose and doesn't actually include the new column into the table:

SELECT *, concat(First_name, " ", Last_name) AS Full_Name FROM Employees;


Solution

  • first you need to add column with Full_Name then update the Full_Name column

    ALTER TABLE Employees
    ADD COLUMN Full_Name VARCHAR(100);
    
    UPDATE Employees 
    SET Full_Name = CONCAT(First_Name, " ", Last_Name);