Search code examples
mysqlsqldatabaseconcatenationalter

How to I add a column to a table, that combines strings from other columns?


I am using Mysql Browser.

I have a table called "users". I am looking to create a column called, "opp". I know how to do this, with a simple Alter Table, Add statement. Where I am struggling is figuring out how to add data to the column. I want the column to be a combination of strings using concat().

The column should end looking like:

"Team(name)" where name is data from a column titled "name" in the table "users".

I have tried a Insert into statement like this:

Insert Into users (opp) VALUES ('Team', '(', name, ')')

But that did not work. It tells me there is no such column as "name", even though there is...

Thanks for the help!


Solution

  • Once you have altered the table you should use an UPDATE

    update  users 
    set opp = concat( 'team(',name ,')' )
    ;
    

    anyway column like this should not be store .. because you can obatin this with simply

    select  concat( 'team(',name ,')' ) opp from user