简体   繁体   English

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

[英]MySql - Way to update portion of a string?

I'm looking for a way to update just a portion of a string via MySQL query.我正在寻找一种方法来通过 MySQL 查询更新字符串的一部分。

For example, if I have 10 records all containing 'string' as part of the field value (ie, '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'例如,如果我有 10 条记录都包含“字符串”作为字段值的一部分(即,“某事/字符串”、“某事/字符串看这里”、“某事/字符串/等等”,有没有办法更改“字符串” ' 通过一个查询将每一行的 'anothervalue' 转换为 'something/anothervalue'、'something/anothervaluelookhere'、'something/string/etcetera',有没有办法改变 'anothervalue'

I think this should work:我认为这应该有效:

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

Use the LIKE operator to find the rows that you care about and update them using the REPLACE function.使用LIKE运算符查找您关心的行并使用REPLACE函数更新它们。

For example:例如:

UPDATE table_name SET field_name = REPLACE(field_name,'search','replace') WHERE field_name LIKE '%some_value%'

Does something like this work in any way?像这样的事情有什么作用吗?

update table_name
set column_name = replace(column_name, 'string%', 'string') 
where column_name like '%string%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM