简体   繁体   中英

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.

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'

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.

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%'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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