将数据附加到已包含数据的 MySQL 数据库字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2761583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending data to a MySQL database field that already has data in it
提问by Graham
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?
我需要将数据“添加”到已经包含数据的字段中,而不会删除当前存在的数据。例如,如果该字段包含 HTML,我需要向该字段添加额外的 HTML。是否有执行此操作的 SQL 调用,或者我是否需要调用该字段中的数据,将新数据连接到现有数据,然后将其重新加载到数据库中?
回答by Mitch Dempsey
UPDATE Table SET Field=CONCAT(Field,'your extra html');
回答by Jeff B
UPDATE myTable SET html=concat(html,'<b>More HTML</b>') WHERE id='10'
... for example. Your WHERE would be different of course.
... 例如。你的 WHERE 当然会有所不同。
回答by powtac
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.
CONCAT_WS()
附加由给定分隔符分隔的多个字符串。CHAR(10, UTF8)
是换行符。
回答by Urvish
UPDATE Table SET Field=CONCAT(IFNULL(Field, ''), 'Your extra HTML')
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.
如果该字段包含 NULL 值,则 CONCAT 也将返回 NULL。使用 IFNULL 将帮助您更新列,即使它具有 NULL 值。