RachelD
RachelD

Reputation: 4089

MySQL add to the contents of a column in an UPDATE statement

Im not sure if this is possible and I'm not finding documentation on it so I thought I would ask the experts.

Is there a way in a MySQL query to add to the existing contents of a column. For example say I have the following table:

table

id     name     value
1      Bob      red

I would like to use a query to add more to the value column while preserving the value that is already there. So for example:

UPDATE `yable` SET `value` += ',blue' WHERE `id` = 1;

Would update the row to the following:

table

id     name     value
1      Bob      red,blue

Is this possible or do I need to use a different language (like PHP) to concatenate the string before updating?

Upvotes: 1

Views: 3172

Answers (1)

Gonzalo.-
Gonzalo.-

Reputation: 12682

UPDATE `yable` SET `value`= `value` + ',blue' WHERE `id` = 1;

is another way to write it.

+= 

is a short way to write that.

also you got function CONCAT()

UPDATE `yable` SET `value`= CONCAT(value,',blue') WHERE `id` = 1;

Upvotes: 3

Related Questions