MySQL - ¿Cómo actualizar una porción de una cadena?


Estoy buscando una manera de actualizar solo una porción de una cadena a través de una consulta MySQL.

Por ejemplo, si tengo 10 registros que contienen todos 'string' como parte del valor del campo (es decir, 'algo/string', 'algo/stringlookhere', 'algo/string/etcetera', hay una manera de cambiar 'string' a 'anothervalue' para cada fila a través de una consulta, de modo que el resultado sea 'algo/anothervalue', 'algo/anothervaluelookhere', 'algo/string/etcetera', cambio 'anothervalue'

Author: Juan Mellado, 2009-12-09

3 answers

Creo que esto debería funcionar:

UPDATE table SET field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';
 191
Author: Kaleb Brasee,
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-01-05 08:50:19
UPDATE `table` SET `field` = REPLACE(`field`, 'string', 'anothervalue')
 25
Author: Tatu Ulmanen,
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-03-07 14:25:25

Utilice el operador LIKE para encontrar las filas que le interesan y actualizarlas utilizando la función REPLACE.

Por ejemplo:

UPDATE table_name SET field_name = REPLACE(field_name,'search','replace') WHERE field_name LIKE '%some_value%'
 14
Author: Bernard Chen,
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-06-29 14:16:43