How to update a column from database , but just to add something to the current value

Go To StackoverFlow.com

1

Let say I have something like this:

id |    title   |

1  |  First row |

Now I want to update that value to be: First row is here, by just adding is here. And as you think, there is more than one row, and I want to update all the rows dynamically.

UPDATE posts SET title=title+' is here'

I know that the above is wrong, I just thought that since it works with numbers, maybe it will also with text, but it doesn't.

2012-04-04 17:05
by Ben
Numeric: id = id + 1. String: use one/some of the string functions MySQL has, e.g. concat or similar - hakre 2012-04-04 17:07


0

use concat:

UPDATE posts SET title=concat(title,'your_string_to_add') WHERE id='your_id' 

It is important to give WHERE id = 'id' otherwise it will update the first row I beleive. In your case:

UPDATE posts SET title=concat(title,' is here') WHERE id=1
2012-04-04 17:11
by Saeid Yazdani


6

To do that you need to concatenate strings, MySQL has the function CONCAT(), so your query would be:

UPDATE posts SET title=CONCAT(title,' is here') 
2012-04-04 17:09
by aurbano


2

UPDATE posts SET title=CONCAT(title,' is here')
2012-04-04 17:09
by Luca Rainone
Ads