42

I need to "add" data to a field that already contains data without erasing whats currently there. For example if the field contains HTML, I need to add additional HTML to the field. Is there a SQL call that will do this or do I need to call the data in that field, concatenate the new data to the existing data, and reload it into the database?

Graham
  • 1,145
  • 4
  • 16
  • 25
  • almost duplicate of http://stackoverflow.com/questions/680801/how-to-prepend-a-string-to-a-column-value-in-mysql - it is about prepending – qdinar Apr 09 '17 at 15:33

4 Answers4

84
UPDATE Table SET Field=CONCAT(Field,'your extra html');
Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
12
UPDATE myTable SET html=concat(html,'<b>More HTML</b>') WHERE id='10' 

... for example. Your WHERE would be different of course.

Jeff B
  • 29,943
  • 7
  • 61
  • 90
4

Append at the end of a field, separated with with a line break:

UPDATE Table SET Comment = CONCAT_WS(CHAR(10 USING UTF8), Comment, 'my comment.');
  • CONCAT_WS() appends multiple strings separated by a given separator.
  • CHAR(10, UTF8) is a line break.
powtac
  • 40,542
  • 28
  • 115
  • 170
4

UPDATE Table SET Field=CONCAT(IFNULL(Field, ''), 'Your extra HTML')

If the field contains NULL value then CONCAT will also return NULL. Using IFNULL will help you to update column even it has NULL value.

Urvish
  • 126
  • 4
  • This instruction appends to all rows. If you want to append only in one specific row add this at the end of the expression: `WHERE your_row = $your_var`. – statosdotcom Jun 22 '22 at 01:25