Search and Replace from another table in MySQL

Posted on March 29, 2013

In MySQL, its easy to find occurrence of a sub-string within string and replace all that occurrences with MySQL REPLACE() function like:


UPDATE tablename set table_column = REPLACE (table_column, '[string to be searched ]', 'string to be replaced');

Also, REPLACE() function works well with SELECT query.


SELECT REPLACE (table_column, '[string to be searched ]', 'string to be replaced') AS new_string FROM table_name;

Here, I would like to share simple SQL with which one can find the occurrence of a string in a table and replace that string with the data of another table like:


UPDATE table1 t1 SET table1_column = REPLACE( table1_column, '[STRING TO BE SEARCHED]', ( SELECT table2_column_name FROM table2 t2 ) );