MySQL buscar y reemplazar algún texto en un campo


¿Qué consulta MySQL hará una búsqueda de texto y reemplazará en un campo particular de una tabla?

Es decir, buscar foo y reemplazar por bar para que un registro con un campo con el valor hello foo se convierta en hello bar.

Author: shgnInc, 2008-09-24

7 answers

Cambie table_name y field para que coincida con el nombre de la tabla y el campo en cuestión:

UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0;
 432
Author: Joe Skora,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-07 11:20:31
UPDATE table_name 
SET field = replace(field, 'string-to-find', 'string-that-will-replace-it');
 78
Author: thesmallprint,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-03 07:23:43

Y si desea buscar y reemplazar basado en el valor de otro campo que podría hacer un CONCAT:

update table_name set `field_name` = replace(`field_name`,'YOUR_OLD_STRING',CONCAT('NEW_STRING',`OTHER_FIELD_VALUE`,'AFTER_IF_NEEDED'));

Solo para tener este aquí para que otros lo encuentren a la vez.

 5
Author: basdog22,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-05-05 09:18:39
 UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);

Como por ejemplo, si quiero reemplazar todas las apariciones de Juan por Marcos voy a utilizar a continuación,

UPDATE student SET student_name = replace(student_name, 'John', 'Mark');
 5
Author: Umesh Patil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-08 10:53:37

La función Replace string hará eso.

 1
Author: Wayne,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-12-01 01:06:38

Usé la línea de comandos anterior de la siguiente manera: actualizar EL NOMBRE DE LA TABLA establecer CAMPO = reemplazar (CAMPO, 'Y','y'); el propósito era reemplazar Y por y ("A" debe ser minúscula). El problema es que no puede encontrar el " Y " en la base de datos, pero si uso como "%Y%", entonces puede encontrarlo junto con muchos otros and que son parte de una palabra o incluso los que ya están en minúsculas.

 0
Author: Schwann,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-22 23:33:30

En mi experiencia, el método más rápido es

UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE field LIKE '%foo%';

La vía INSTR() es la segunda más rápida y omitir la cláusula WHERE por completo es la más lenta, incluso si la columna no está indexada.

 0
Author: Gaspy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-05 14:07:21