MYSql multiple replace query MYSql multiple replace query mysql mysql

MYSql multiple replace query


Matryoshka-way ;-)

REPLACE(REPLACE(REPLACE(example, '3', 'test') , '2', 'test') , '1', 'test') 


A stored procedure.

Given you have a table 'lut' with a set of values that you want replacing in a field 'content' from a table is called 'example'

delimiter //CREATE PROCEDURE myreplace()BEGINDECLARE done INT DEFAULT FALSE;DECLARE lv CHAR(64);DECLARE li INT;DECLARE lut CURSOR FOR SELECT id,value FROM lut l;DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;OPEN lut;lut_loop: LOOPFETCH lut INTO li,lv;IF done THENLEAVE lut_loop;END IF;update example set content = replace(content,lv,li);END LOOP; CLOSE lut;END;//delimiter ;call myreplace();drop procedure myreplace;