What MySQL query will do a text search and replace in one particular field in a table?
I.e. search for foo and replace with bar so a record with a field with the value hello foo becomes hello bar.
Change table_name and field to match your table name and field in question:
UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0;
[field_name], "foo", "bar");WHERE instr(field, 'foo') > 0; (so it would not perform 2 searches)... Am I wrong?WHERE clause you do an UPDATE on all the rows...UPDATE table_name
SET field = replace(field, 'string-to-find', 'string-that-will-replace-it');
In my experience, the fastest method is
UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE field LIKE '%foo%';
The INSTR() way is the second-fastest and omitting the WHERE clause altogether is slowest, even if the column is not indexed.
And if you want to search and replace based on the value of another field you could do a CONCAT:
update table_name set `field_name` = replace(`field_name`,'YOUR_OLD_STRING',CONCAT('NEW_STRING',`OTHER_FIELD_VALUE`,'AFTER_IF_NEEDED'));
Just to have this one here so that others will find it at once.