MySQL Find and Replace

This is a good “oh shit” tool… It is a simple way to do a search and replace across all values in a MySQL database column. It will replace every occurrence in the column so use with caution…

UPDATE `table` SET `column` = REPLACE(`column`, 'find', 'replace')

I use this often to update WordPress when I am moving a site from staging to a live server. In the following example I update the wp_posts table to replace the staging url with the live url.

UPDATE `wp_posts` SET `post_content` = REPLACE(`post_content`, '', '');
UPDATE `wp_posts` SET `guid` = REPLACE(`guid`, '', '');

About this entry