Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL UPDATE append data into column

I need to UPDATE tablename (col1name)

If there is already data, I need to append it with values 'a,b,c' If it is NULL, I need to add the values 'a,b,c'

I know there is a CONCAT argument, but not sure what the SQL syntax would be.

update tablename set col1name = concat(ifnull(col1name, 'a,b,c'), 'a,b,c')

Is the above correct?

like image 957
Robert Hoffmann Avatar asked Dec 24 '12 11:12

Robert Hoffmann


People also ask

How do I append a record in MySQL?

You can append data to a MySQL database field with the help of in-built CONCAT() function. Here is the query to append the data ”Taylor” to the data already in the column. Therefore, the data will get appended.

How can I append a string to an existing field in MySQL?

To prepend a string to a column value in MySQL, we can use the function CONCAT. The CONCAT function can be used with UPDATE statement.


1 Answers

Try this Query:

update tablename set col1name = concat(ifnull(col1name,""), 'a,b,c'); 

Refer this sql fiddle demo.

like image 134
Dhinakar Avatar answered Oct 09 '22 05:10

Dhinakar