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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ben
  • 1,906
  • 10
  • 31
  • 47
  • Numeric: id = id + 1. String: use one/some of the string functions MySQL has, e.g. concat or similar. – hakre Apr 04 '12 at 17:07
  • possible duplicate of [SQL UPDATE all values in a field with appended string CONCAT not working](http://stackoverflow.com/questions/4128335/sql-update-all-values-in-a-field-with-appended-string-concat-not-working) – hakre Apr 04 '12 at 17:10

3 Answers3

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') 
aurbano
  • 3,324
  • 1
  • 25
  • 39
2
UPDATE posts SET title=CONCAT(title,' is here')
Luca Rainone
  • 16,138
  • 2
  • 38
  • 52
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
Dumbo
  • 13,555
  • 54
  • 184
  • 288