如何在 MySQL 中将字符串添加到列值之前?

2021-11-20 00:00:00 sql-update mysql

我需要一个 SQL 更新语句来更新所有行的特定字段,并在现有值的前面添加一个字符串test".

I need a SQL update statement for updating a particular field of all the rows with a string "test" to be added in the front of the existing value.

例如,如果现有值是try",它应该变成testtry".

For example, if the existing value is "try" it should become "testtry".

推荐答案

您可以使用 CONCAT 函数来做到这一点:

You can use the CONCAT function to do that:

UPDATE tbl SET col=CONCAT('test',col);

如果您想变得更聪明并且只更新尚未预先设置测试的列,请尝试

If you want to get cleverer and only update columns which don't already have test prepended, try

UPDATE tbl SET col=CONCAT('test',col)
WHERE col NOT LIKE 'test%';

相关文章