MySql - 更新字符串部分的方法?

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

我正在寻找一种方法来通过 MySQL 查询更新字符串的一部分.

I'm looking for a way to update just a portion of a string via MySQL query.

例如,如果我有 10 条记录,所有记录都包含字符串"作为字段值的一部分(即,某事/字符串"、某事/字符串看这里"、某事/字符串/等",有没有办法通过一个查询将每一行的 'string' 更改为 'anothervalue',以便结果是 'something/anothervalue'、'something/anothervaluelookhere'、'something/string/etcetera',有没有办法改变 'anothervalue'

For example, if I have 10 records all containing 'string' as part of the field value (i.e., 'something/string', 'something/stringlookhere', 'something/string/etcetera', is there a way to change 'string' to 'anothervalue' for each row via one query, so that the result is 'something/anothervalue', 'something/anothervaluelookhere', 'something/string/etcetera', is there a way to change 'anothervalue'

推荐答案

我认为这应该可行:

UPDATE table
SET field = REPLACE(field, 'string', 'anothervalue')
WHERE field LIKE '%string%';

相关文章