MySQL: Search and Replace

Well, After spending a lot of time trying to work this out, I thought I’d post this here to save others the trouble that I had. Virtually what you do here is run this as a MySQL query and what it will do is search for a term and replace it with something else, which could come in quite handy should you ever need to do a mass edit. The query is;

UPDATE tablename SET tablefield = replace(tablefield,”findstring”,”replacestring”);

So to explain this really easy replace table with the table name inside your database, column with the column inside the table you want to search and replace in, do the same again. Then replace search with what you want to search for and replace with what you want to replace it with. It couldn’t get much easier if it tried. I hope this helps you all out as it helped me out a lot by editing all these blog posts and updating the upload paths to all the uploaded blog pictures. To finish off this post, I’m going to leave you with the query I had to run as an example to help you all out.

UPDATE wp_posts SET post_content = replace(post_content,”http://sheppardsblog.com/wp-content/uploads/”,”http://shaunlorrain.com/wp-content/uploads/”);

UPDATE tablename SET tablefield = replace(tablefield,"findstring","replacestring");

Shaun

Love . Student, IT Consultant, Web Master and social media junkie. Interests include classical music, photography and gaming. Have Asperger’s and MDD.

Leave a Reply