如何从数据库中更新列,但只是向当前值添加一些内容


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

假设我有这样的东西:

id |    title   |
1  |  First row |

现在我想将该值更新为:第一行在这里,只需添加就在这里。正如您所想,有不止一行,我想动态更新所有行。

UPDATE posts SET title=title+' is here'

我知道上面的内容是错误的,我只是觉得既然它适用于数字,也许它也适用于文本,但事实并非如此。

要做到这一点,您需要连接字符串,MySQL有CONCAT()函数,所以您的查询将是:

UPDATE posts SET title=CONCAT(title,' is here') 
UPDATE posts SET title=CONCAT(title,' is here')

使用concat:

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

重要的是给出WHERE id = 'id',否则它将更新I所引用的第一行。在您的情况下:

UPDATE posts SET title=concat(title,' is here') WHERE id=1