Find and Replace text in the entire table using a MySQL query










202














Usually I use manual find to replace text in a MySQL database using phpmyadmin. I'm tired of it now, how can I run a query to find and replace a text with new text in the entire table in phpmyadmin?



Example: find keyword domain.com, replace with www.domain.com.










share|improve this question























  • Possible duplicate of stackoverflow.com/questions/639531/…
    – sel
    Aug 7 '12 at 4:05






  • 1




    You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
    – Pramod
    Apr 16 '13 at 6:50






  • 1




    This will help you achieve what you need.
    – Dom
    Aug 25 '14 at 20:07










  • Possible duplicate of MySQL string replace
    – Steve Chambers
    Aug 2 '16 at 14:02















202














Usually I use manual find to replace text in a MySQL database using phpmyadmin. I'm tired of it now, how can I run a query to find and replace a text with new text in the entire table in phpmyadmin?



Example: find keyword domain.com, replace with www.domain.com.










share|improve this question























  • Possible duplicate of stackoverflow.com/questions/639531/…
    – sel
    Aug 7 '12 at 4:05






  • 1




    You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
    – Pramod
    Apr 16 '13 at 6:50






  • 1




    This will help you achieve what you need.
    – Dom
    Aug 25 '14 at 20:07










  • Possible duplicate of MySQL string replace
    – Steve Chambers
    Aug 2 '16 at 14:02













202












202








202


87





Usually I use manual find to replace text in a MySQL database using phpmyadmin. I'm tired of it now, how can I run a query to find and replace a text with new text in the entire table in phpmyadmin?



Example: find keyword domain.com, replace with www.domain.com.










share|improve this question















Usually I use manual find to replace text in a MySQL database using phpmyadmin. I'm tired of it now, how can I run a query to find and replace a text with new text in the entire table in phpmyadmin?



Example: find keyword domain.com, replace with www.domain.com.







mysql phpmyadmin






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '16 at 14:41









guaka

10.2k74481




10.2k74481










asked Aug 7 '12 at 3:59









alyasabrina

1,037287




1,037287











  • Possible duplicate of stackoverflow.com/questions/639531/…
    – sel
    Aug 7 '12 at 4:05






  • 1




    You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
    – Pramod
    Apr 16 '13 at 6:50






  • 1




    This will help you achieve what you need.
    – Dom
    Aug 25 '14 at 20:07










  • Possible duplicate of MySQL string replace
    – Steve Chambers
    Aug 2 '16 at 14:02
















  • Possible duplicate of stackoverflow.com/questions/639531/…
    – sel
    Aug 7 '12 at 4:05






  • 1




    You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
    – Pramod
    Apr 16 '13 at 6:50






  • 1




    This will help you achieve what you need.
    – Dom
    Aug 25 '14 at 20:07










  • Possible duplicate of MySQL string replace
    – Steve Chambers
    Aug 2 '16 at 14:02















Possible duplicate of stackoverflow.com/questions/639531/…
– sel
Aug 7 '12 at 4:05




Possible duplicate of stackoverflow.com/questions/639531/…
– sel
Aug 7 '12 at 4:05




1




1




You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
– Pramod
Apr 16 '13 at 6:50




You can do some thing like [this][1]. [1]: stackoverflow.com/questions/562457/…
– Pramod
Apr 16 '13 at 6:50




1




1




This will help you achieve what you need.
– Dom
Aug 25 '14 at 20:07




This will help you achieve what you need.
– Dom
Aug 25 '14 at 20:07












Possible duplicate of MySQL string replace
– Steve Chambers
Aug 2 '16 at 14:02




Possible duplicate of MySQL string replace
– Steve Chambers
Aug 2 '16 at 14:02












9 Answers
9






active

oldest

votes


















481














For a single table update



 UPDATE `table_name`
SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')


From multiple tables-



If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back.






share|improve this answer


















  • 3




    Does this replace an entire field, or with it do a substring match within a field?
    – Randy Greencorn
    Nov 14 '13 at 21:09






  • 2




    It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
    – Andrew
    Aug 15 '16 at 20:34






  • 7




    and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
    – michelek
    Dec 16 '16 at 0:59






  • 2




    More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
    – kakoma
    Sep 2 '17 at 13:01







  • 1




    Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
    – Heres2u
    Mar 20 at 14:34


















28














The easiest way I have found is to dump the database to a text file, run a sed command to do the replace, and reload the database back into MySQL.



All commands are bash on Linux, from memory.



Dump database to text file



mysqldump -u user -p databasename > ./db.sql


Run sed command to find/replace target string



sed -i 's/oldString/newString/g' ./db.sql


Reload the database into MySQL



mysql -u user -p databasename < ./db.sql


Easy peasy.






share|improve this answer


















  • 1




    This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
    – Mike Kormendy
    Feb 9 '15 at 4:49






  • 1




    It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
    – m.cichacz
    Aug 25 '16 at 8:52







  • 1




    Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
    – afterglowlee
    Jan 31 '17 at 22:05







  • 1




    this one suits my need, thank you!
    – budiantoip
    Aug 1 '17 at 5:01


















23














Put this in a php file and run it and it should do what you want it to do.



// Connect to your MySQL database.
$hostname = "localhost";
$username = "db_username";
$password = "db_password";
$database = "db_name";

mysql_connect($hostname, $username, $password);

// The find and replace strings.
$find = "find_this_text";
$replace = "replace_with_this_text";

$loop = mysql_query("
SELECT
concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''$find'', ''$replace'');') AS s
FROM
information_schema.columns
WHERE
table_schema = '$database'")
or die ('Cant loop through dbfields: ' . mysql_error());

while ($query = mysql_fetch_assoc($loop))

mysql_query($query['s']);






share|improve this answer




























    20














    Running an SQL query in PHPmyadmin to find and replace text in all wordpress blog posts, such as finding mysite.com/wordpress and replacing that with mysite.com/news
    Table in this example is tj_posts



    UPDATE `tj_posts`
    SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')





    share|improve this answer






















    • Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
      – Maris B.
      Jul 25 at 12:39



















    8














     UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);


    Like for example, if I want to replace all occurrences of John by Mark I will use below,



    UPDATE student SET student_name = replace(student_name, 'John', 'Mark');





    share|improve this answer




























      6














      Another option is to generate the statements for each column in the database:



      SELECT CONCAT(
      'update ', table_name ,
      ' set ', column_name, ' = replace(', column_name,', ''www.oldDomain.com'', ''www.newDomain.com'');'
      ) AS statement
      FROM information_schema.columns
      WHERE table_schema = 'mySchema' AND table_name LIKE 'yourPrefix_%';


      This should generate a list of update statements that you can then execute.






      share|improve this answer


















      • 2




        Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
        – Stephane
        Aug 14 '16 at 13:36






      • 2




        This is by far the best approach, if you have a lot of data and cannot dump/reload it.
        – Kariem
        Mar 20 '17 at 8:57










      • Oh man this worked great! I will share my scripts based on this idea
        – Andy
        Nov 3 '17 at 17:40


















      2














      I believe "swapnesh" answer to be the best ! Unfortunately I couldn't execute it in phpMyAdmin (4.5.0.2) who although illogical (and tried several things) it kept saying that a new statement was found and that no delimiter was found…



      Thus I came with the following solution that might be usefull if you exeprience the same issue and have no other access to the database than PMA…



      UPDATE `wp_posts` AS `toUpdate`,
      (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
      FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
      SET `toUpdate`.`guid`=`updated`.`guid`
      WHERE `toUpdate`.`ID`=`updated`.`ID`;


      To test the expected result you may want to use :



      SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
      FROM `wp_posts` AS `toUpdate`,
      (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
      FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
      WHERE `toUpdate`.`ID`=`updated`.`ID`;





      share|improve this answer




























        0














        Generate change SQL queries (FAST)



        mysql -e "SELECT CONCAT( 'update ', table_name , ' set ', column_name, ' = replace(', column_name,', ''www.oldsite.com'', ''www.newsite.com'');' ) AS statement FROM information_schema.columns WHERE table_name LIKE 'wp_%'" -u root -p your_db_name_here > upgrade_script.sql



        Remove any garbage at the start of the file. I had some.



        nano upgrade_script.sql



        Run generated script with --force options to skip errors. (SLOW - grab a coffee if big DB)



        mysql -u root -p your_db_name_here --force < upgrade_script.sql






        share|improve this answer




























          0














          the best you export it as sql file and open it with editor such as visual studio code and find and repalace your words.
          i replace in 1 gig file sql in 1 minutes for 16 word that total is 14600 word.
          its the best way.
          and after replace it save and import it again.
          do not forget compress it with zip for import.






          share|improve this answer



















            protected by Community May 29 '14 at 19:25



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            9 Answers
            9






            active

            oldest

            votes








            9 Answers
            9






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            481














            For a single table update



             UPDATE `table_name`
            SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')


            From multiple tables-



            If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back.






            share|improve this answer


















            • 3




              Does this replace an entire field, or with it do a substring match within a field?
              – Randy Greencorn
              Nov 14 '13 at 21:09






            • 2




              It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
              – Andrew
              Aug 15 '16 at 20:34






            • 7




              and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
              – michelek
              Dec 16 '16 at 0:59






            • 2




              More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
              – kakoma
              Sep 2 '17 at 13:01







            • 1




              Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
              – Heres2u
              Mar 20 at 14:34















            481














            For a single table update



             UPDATE `table_name`
            SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')


            From multiple tables-



            If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back.






            share|improve this answer


















            • 3




              Does this replace an entire field, or with it do a substring match within a field?
              – Randy Greencorn
              Nov 14 '13 at 21:09






            • 2




              It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
              – Andrew
              Aug 15 '16 at 20:34






            • 7




              and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
              – michelek
              Dec 16 '16 at 0:59






            • 2




              More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
              – kakoma
              Sep 2 '17 at 13:01







            • 1




              Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
              – Heres2u
              Mar 20 at 14:34













            481












            481








            481






            For a single table update



             UPDATE `table_name`
            SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')


            From multiple tables-



            If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back.






            share|improve this answer














            For a single table update



             UPDATE `table_name`
            SET `field_name` = replace(same_field_name, 'unwanted_text', 'wanted_text')


            From multiple tables-



            If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 7 '12 at 4:45

























            answered Aug 7 '12 at 4:06









            swapnesh

            17.7k2173108




            17.7k2173108







            • 3




              Does this replace an entire field, or with it do a substring match within a field?
              – Randy Greencorn
              Nov 14 '13 at 21:09






            • 2




              It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
              – Andrew
              Aug 15 '16 at 20:34






            • 7




              and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
              – michelek
              Dec 16 '16 at 0:59






            • 2




              More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
              – kakoma
              Sep 2 '17 at 13:01







            • 1




              Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
              – Heres2u
              Mar 20 at 14:34












            • 3




              Does this replace an entire field, or with it do a substring match within a field?
              – Randy Greencorn
              Nov 14 '13 at 21:09






            • 2




              It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
              – Andrew
              Aug 15 '16 at 20:34






            • 7




              and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
              – michelek
              Dec 16 '16 at 0:59






            • 2




              More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
              – kakoma
              Sep 2 '17 at 13:01







            • 1




              Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
              – Heres2u
              Mar 20 at 14:34







            3




            3




            Does this replace an entire field, or with it do a substring match within a field?
            – Randy Greencorn
            Nov 14 '13 at 21:09




            Does this replace an entire field, or with it do a substring match within a field?
            – Randy Greencorn
            Nov 14 '13 at 21:09




            2




            2




            It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
            – Andrew
            Aug 15 '16 at 20:34




            It will replace a substring within the field @RandyGreencorn . It's also case-sensitive.
            – Andrew
            Aug 15 '16 at 20:34




            7




            7




            and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
            – michelek
            Dec 16 '16 at 0:59




            and it will replace 'domain.com' with 'www.domain.com' and 'www.domain.com' with 'www.www.domain.com'
            – michelek
            Dec 16 '16 at 0:59




            2




            2




            More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
            – kakoma
            Sep 2 '17 at 13:01





            More on this: If you want to edit from all tables, best way is to take the dump and then find/replace and upload it back. Use sed on the dump for the find/replace: sed "s:unwanted_text:wanted_text:g" dump.sql
            – kakoma
            Sep 2 '17 at 13:01





            1




            1




            Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
            – Heres2u
            Mar 20 at 14:34




            Works great. As others have stated, sometimes I have to mess with the quotes to get it to work in phpMyAdmin. I used it to replace only the text "http:" with "https:" in a column containing full web addresses. The rest of the web addresses were untouched.
            – Heres2u
            Mar 20 at 14:34













            28














            The easiest way I have found is to dump the database to a text file, run a sed command to do the replace, and reload the database back into MySQL.



            All commands are bash on Linux, from memory.



            Dump database to text file



            mysqldump -u user -p databasename > ./db.sql


            Run sed command to find/replace target string



            sed -i 's/oldString/newString/g' ./db.sql


            Reload the database into MySQL



            mysql -u user -p databasename < ./db.sql


            Easy peasy.






            share|improve this answer


















            • 1




              This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
              – Mike Kormendy
              Feb 9 '15 at 4:49






            • 1




              It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
              – m.cichacz
              Aug 25 '16 at 8:52







            • 1




              Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
              – afterglowlee
              Jan 31 '17 at 22:05







            • 1




              this one suits my need, thank you!
              – budiantoip
              Aug 1 '17 at 5:01















            28














            The easiest way I have found is to dump the database to a text file, run a sed command to do the replace, and reload the database back into MySQL.



            All commands are bash on Linux, from memory.



            Dump database to text file



            mysqldump -u user -p databasename > ./db.sql


            Run sed command to find/replace target string



            sed -i 's/oldString/newString/g' ./db.sql


            Reload the database into MySQL



            mysql -u user -p databasename < ./db.sql


            Easy peasy.






            share|improve this answer


















            • 1




              This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
              – Mike Kormendy
              Feb 9 '15 at 4:49






            • 1




              It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
              – m.cichacz
              Aug 25 '16 at 8:52







            • 1




              Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
              – afterglowlee
              Jan 31 '17 at 22:05







            • 1




              this one suits my need, thank you!
              – budiantoip
              Aug 1 '17 at 5:01













            28












            28








            28






            The easiest way I have found is to dump the database to a text file, run a sed command to do the replace, and reload the database back into MySQL.



            All commands are bash on Linux, from memory.



            Dump database to text file



            mysqldump -u user -p databasename > ./db.sql


            Run sed command to find/replace target string



            sed -i 's/oldString/newString/g' ./db.sql


            Reload the database into MySQL



            mysql -u user -p databasename < ./db.sql


            Easy peasy.






            share|improve this answer














            The easiest way I have found is to dump the database to a text file, run a sed command to do the replace, and reload the database back into MySQL.



            All commands are bash on Linux, from memory.



            Dump database to text file



            mysqldump -u user -p databasename > ./db.sql


            Run sed command to find/replace target string



            sed -i 's/oldString/newString/g' ./db.sql


            Reload the database into MySQL



            mysql -u user -p databasename < ./db.sql


            Easy peasy.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 19 '15 at 2:10

























            answered Jan 19 '15 at 1:55









            siliconrockstar

            2,2462831




            2,2462831







            • 1




              This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
              – Mike Kormendy
              Feb 9 '15 at 4:49






            • 1




              It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
              – m.cichacz
              Aug 25 '16 at 8:52







            • 1




              Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
              – afterglowlee
              Jan 31 '17 at 22:05







            • 1




              this one suits my need, thank you!
              – budiantoip
              Aug 1 '17 at 5:01












            • 1




              This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
              – Mike Kormendy
              Feb 9 '15 at 4:49






            • 1




              It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
              – m.cichacz
              Aug 25 '16 at 8:52







            • 1




              Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
              – afterglowlee
              Jan 31 '17 at 22:05







            • 1




              this one suits my need, thank you!
              – budiantoip
              Aug 1 '17 at 5:01







            1




            1




            This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
            – Mike Kormendy
            Feb 9 '15 at 4:49




            This works amazingly fast. I love this solution. I had to do some url replacements and instead of using slashes as my delimiters I used pipes instead (read this up grymoire.com/Unix/Sed.html). Example: sed -i 's|olddomain.com|http://newdomain.com|g' ./db.sql
            – Mike Kormendy
            Feb 9 '15 at 4:49




            1




            1




            It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
            – m.cichacz
            Aug 25 '16 at 8:52





            It was so fast i thought it didn't work. But it did! Also, you can just escape slashes like this: //domain.com
            – m.cichacz
            Aug 25 '16 at 8:52





            1




            1




            Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
            – afterglowlee
            Jan 31 '17 at 22:05





            Just a reminder that in OS X, the sed -i command may throw out unterminated substitute pattern error. You can use sed -i '' -e 's/oldString/newString/g' ./db.sql instead.
            – afterglowlee
            Jan 31 '17 at 22:05





            1




            1




            this one suits my need, thank you!
            – budiantoip
            Aug 1 '17 at 5:01




            this one suits my need, thank you!
            – budiantoip
            Aug 1 '17 at 5:01











            23














            Put this in a php file and run it and it should do what you want it to do.



            // Connect to your MySQL database.
            $hostname = "localhost";
            $username = "db_username";
            $password = "db_password";
            $database = "db_name";

            mysql_connect($hostname, $username, $password);

            // The find and replace strings.
            $find = "find_this_text";
            $replace = "replace_with_this_text";

            $loop = mysql_query("
            SELECT
            concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''$find'', ''$replace'');') AS s
            FROM
            information_schema.columns
            WHERE
            table_schema = '$database'")
            or die ('Cant loop through dbfields: ' . mysql_error());

            while ($query = mysql_fetch_assoc($loop))

            mysql_query($query['s']);






            share|improve this answer

























              23














              Put this in a php file and run it and it should do what you want it to do.



              // Connect to your MySQL database.
              $hostname = "localhost";
              $username = "db_username";
              $password = "db_password";
              $database = "db_name";

              mysql_connect($hostname, $username, $password);

              // The find and replace strings.
              $find = "find_this_text";
              $replace = "replace_with_this_text";

              $loop = mysql_query("
              SELECT
              concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''$find'', ''$replace'');') AS s
              FROM
              information_schema.columns
              WHERE
              table_schema = '$database'")
              or die ('Cant loop through dbfields: ' . mysql_error());

              while ($query = mysql_fetch_assoc($loop))

              mysql_query($query['s']);






              share|improve this answer























                23












                23








                23






                Put this in a php file and run it and it should do what you want it to do.



                // Connect to your MySQL database.
                $hostname = "localhost";
                $username = "db_username";
                $password = "db_password";
                $database = "db_name";

                mysql_connect($hostname, $username, $password);

                // The find and replace strings.
                $find = "find_this_text";
                $replace = "replace_with_this_text";

                $loop = mysql_query("
                SELECT
                concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''$find'', ''$replace'');') AS s
                FROM
                information_schema.columns
                WHERE
                table_schema = '$database'")
                or die ('Cant loop through dbfields: ' . mysql_error());

                while ($query = mysql_fetch_assoc($loop))

                mysql_query($query['s']);






                share|improve this answer












                Put this in a php file and run it and it should do what you want it to do.



                // Connect to your MySQL database.
                $hostname = "localhost";
                $username = "db_username";
                $password = "db_password";
                $database = "db_name";

                mysql_connect($hostname, $username, $password);

                // The find and replace strings.
                $find = "find_this_text";
                $replace = "replace_with_this_text";

                $loop = mysql_query("
                SELECT
                concat('UPDATE ',table_schema,'.',table_name, ' SET ',column_name, '=replace(',column_name,', ''$find'', ''$replace'');') AS s
                FROM
                information_schema.columns
                WHERE
                table_schema = '$database'")
                or die ('Cant loop through dbfields: ' . mysql_error());

                while ($query = mysql_fetch_assoc($loop))

                mysql_query($query['s']);







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 12 '14 at 16:58









                Lee Woodman

                95621127




                95621127





















                    20














                    Running an SQL query in PHPmyadmin to find and replace text in all wordpress blog posts, such as finding mysite.com/wordpress and replacing that with mysite.com/news
                    Table in this example is tj_posts



                    UPDATE `tj_posts`
                    SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')





                    share|improve this answer






















                    • Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                      – Maris B.
                      Jul 25 at 12:39
















                    20














                    Running an SQL query in PHPmyadmin to find and replace text in all wordpress blog posts, such as finding mysite.com/wordpress and replacing that with mysite.com/news
                    Table in this example is tj_posts



                    UPDATE `tj_posts`
                    SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')





                    share|improve this answer






















                    • Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                      – Maris B.
                      Jul 25 at 12:39














                    20












                    20








                    20






                    Running an SQL query in PHPmyadmin to find and replace text in all wordpress blog posts, such as finding mysite.com/wordpress and replacing that with mysite.com/news
                    Table in this example is tj_posts



                    UPDATE `tj_posts`
                    SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')





                    share|improve this answer














                    Running an SQL query in PHPmyadmin to find and replace text in all wordpress blog posts, such as finding mysite.com/wordpress and replacing that with mysite.com/news
                    Table in this example is tj_posts



                    UPDATE `tj_posts`
                    SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Feb 19 '14 at 2:44









                    Hooked

                    45.5k26133203




                    45.5k26133203










                    answered Nov 5 '13 at 2:57









                    guest

                    20122




                    20122











                    • Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                      – Maris B.
                      Jul 25 at 12:39

















                    • Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                      – Maris B.
                      Jul 25 at 12:39
















                    Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                    – Maris B.
                    Jul 25 at 12:39





                    Thanks for the query. For my WordPress site the column name is wp_posts so the query looks UPDATE `wp_posts` SET `post_content` = replace(post_content, 'mysite.com/wordpress', 'mysite.com/news')
                    – Maris B.
                    Jul 25 at 12:39












                    8














                     UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);


                    Like for example, if I want to replace all occurrences of John by Mark I will use below,



                    UPDATE student SET student_name = replace(student_name, 'John', 'Mark');





                    share|improve this answer

























                      8














                       UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);


                      Like for example, if I want to replace all occurrences of John by Mark I will use below,



                      UPDATE student SET student_name = replace(student_name, 'John', 'Mark');





                      share|improve this answer























                        8












                        8








                        8






                         UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);


                        Like for example, if I want to replace all occurrences of John by Mark I will use below,



                        UPDATE student SET student_name = replace(student_name, 'John', 'Mark');





                        share|improve this answer












                         UPDATE table SET field = replace(field, text_needs_to_be_replaced, text_required);


                        Like for example, if I want to replace all occurrences of John by Mark I will use below,



                        UPDATE student SET student_name = replace(student_name, 'John', 'Mark');






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 8 '14 at 11:11









                        Umesh Patil

                        1,8821816




                        1,8821816





















                            6














                            Another option is to generate the statements for each column in the database:



                            SELECT CONCAT(
                            'update ', table_name ,
                            ' set ', column_name, ' = replace(', column_name,', ''www.oldDomain.com'', ''www.newDomain.com'');'
                            ) AS statement
                            FROM information_schema.columns
                            WHERE table_schema = 'mySchema' AND table_name LIKE 'yourPrefix_%';


                            This should generate a list of update statements that you can then execute.






                            share|improve this answer


















                            • 2




                              Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                              – Stephane
                              Aug 14 '16 at 13:36






                            • 2




                              This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                              – Kariem
                              Mar 20 '17 at 8:57










                            • Oh man this worked great! I will share my scripts based on this idea
                              – Andy
                              Nov 3 '17 at 17:40















                            6














                            Another option is to generate the statements for each column in the database:



                            SELECT CONCAT(
                            'update ', table_name ,
                            ' set ', column_name, ' = replace(', column_name,', ''www.oldDomain.com'', ''www.newDomain.com'');'
                            ) AS statement
                            FROM information_schema.columns
                            WHERE table_schema = 'mySchema' AND table_name LIKE 'yourPrefix_%';


                            This should generate a list of update statements that you can then execute.






                            share|improve this answer


















                            • 2




                              Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                              – Stephane
                              Aug 14 '16 at 13:36






                            • 2




                              This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                              – Kariem
                              Mar 20 '17 at 8:57










                            • Oh man this worked great! I will share my scripts based on this idea
                              – Andy
                              Nov 3 '17 at 17:40













                            6












                            6








                            6






                            Another option is to generate the statements for each column in the database:



                            SELECT CONCAT(
                            'update ', table_name ,
                            ' set ', column_name, ' = replace(', column_name,', ''www.oldDomain.com'', ''www.newDomain.com'');'
                            ) AS statement
                            FROM information_schema.columns
                            WHERE table_schema = 'mySchema' AND table_name LIKE 'yourPrefix_%';


                            This should generate a list of update statements that you can then execute.






                            share|improve this answer














                            Another option is to generate the statements for each column in the database:



                            SELECT CONCAT(
                            'update ', table_name ,
                            ' set ', column_name, ' = replace(', column_name,', ''www.oldDomain.com'', ''www.newDomain.com'');'
                            ) AS statement
                            FROM information_schema.columns
                            WHERE table_schema = 'mySchema' AND table_name LIKE 'yourPrefix_%';


                            This should generate a list of update statements that you can then execute.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Mar 20 '17 at 9:47









                            Kariem

                            2,10222847




                            2,10222847










                            answered Apr 2 '15 at 13:17









                            Farid.O

                            22336




                            22336







                            • 2




                              Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                              – Stephane
                              Aug 14 '16 at 13:36






                            • 2




                              This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                              – Kariem
                              Mar 20 '17 at 8:57










                            • Oh man this worked great! I will share my scripts based on this idea
                              – Andy
                              Nov 3 '17 at 17:40












                            • 2




                              Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                              – Stephane
                              Aug 14 '16 at 13:36






                            • 2




                              This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                              – Kariem
                              Mar 20 '17 at 8:57










                            • Oh man this worked great! I will share my scripts based on this idea
                              – Andy
                              Nov 3 '17 at 17:40







                            2




                            2




                            Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                            – Stephane
                            Aug 14 '16 at 13:36




                            Slower than dumping, but for people that don't feel comfortable with the command line, this answer is creative and effective.
                            – Stephane
                            Aug 14 '16 at 13:36




                            2




                            2




                            This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                            – Kariem
                            Mar 20 '17 at 8:57




                            This is by far the best approach, if you have a lot of data and cannot dump/reload it.
                            – Kariem
                            Mar 20 '17 at 8:57












                            Oh man this worked great! I will share my scripts based on this idea
                            – Andy
                            Nov 3 '17 at 17:40




                            Oh man this worked great! I will share my scripts based on this idea
                            – Andy
                            Nov 3 '17 at 17:40











                            2














                            I believe "swapnesh" answer to be the best ! Unfortunately I couldn't execute it in phpMyAdmin (4.5.0.2) who although illogical (and tried several things) it kept saying that a new statement was found and that no delimiter was found…



                            Thus I came with the following solution that might be usefull if you exeprience the same issue and have no other access to the database than PMA…



                            UPDATE `wp_posts` AS `toUpdate`,
                            (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                            FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                            SET `toUpdate`.`guid`=`updated`.`guid`
                            WHERE `toUpdate`.`ID`=`updated`.`ID`;


                            To test the expected result you may want to use :



                            SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
                            FROM `wp_posts` AS `toUpdate`,
                            (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                            FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                            WHERE `toUpdate`.`ID`=`updated`.`ID`;





                            share|improve this answer

























                              2














                              I believe "swapnesh" answer to be the best ! Unfortunately I couldn't execute it in phpMyAdmin (4.5.0.2) who although illogical (and tried several things) it kept saying that a new statement was found and that no delimiter was found…



                              Thus I came with the following solution that might be usefull if you exeprience the same issue and have no other access to the database than PMA…



                              UPDATE `wp_posts` AS `toUpdate`,
                              (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                              FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                              SET `toUpdate`.`guid`=`updated`.`guid`
                              WHERE `toUpdate`.`ID`=`updated`.`ID`;


                              To test the expected result you may want to use :



                              SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
                              FROM `wp_posts` AS `toUpdate`,
                              (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                              FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                              WHERE `toUpdate`.`ID`=`updated`.`ID`;





                              share|improve this answer























                                2












                                2








                                2






                                I believe "swapnesh" answer to be the best ! Unfortunately I couldn't execute it in phpMyAdmin (4.5.0.2) who although illogical (and tried several things) it kept saying that a new statement was found and that no delimiter was found…



                                Thus I came with the following solution that might be usefull if you exeprience the same issue and have no other access to the database than PMA…



                                UPDATE `wp_posts` AS `toUpdate`,
                                (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                                FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                                SET `toUpdate`.`guid`=`updated`.`guid`
                                WHERE `toUpdate`.`ID`=`updated`.`ID`;


                                To test the expected result you may want to use :



                                SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
                                FROM `wp_posts` AS `toUpdate`,
                                (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                                FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                                WHERE `toUpdate`.`ID`=`updated`.`ID`;





                                share|improve this answer












                                I believe "swapnesh" answer to be the best ! Unfortunately I couldn't execute it in phpMyAdmin (4.5.0.2) who although illogical (and tried several things) it kept saying that a new statement was found and that no delimiter was found…



                                Thus I came with the following solution that might be usefull if you exeprience the same issue and have no other access to the database than PMA…



                                UPDATE `wp_posts` AS `toUpdate`,
                                (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                                FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                                SET `toUpdate`.`guid`=`updated`.`guid`
                                WHERE `toUpdate`.`ID`=`updated`.`ID`;


                                To test the expected result you may want to use :



                                SELECT `toUpdate`.`guid` AS `old guid`,`updated`.`guid` AS `new guid`
                                FROM `wp_posts` AS `toUpdate`,
                                (SELECT `ID`,REPLACE(`guid`,'http://old.tld','http://new.tld') AS `guid`
                                FROM `wp_posts` WHERE `guid` LIKE 'http://old.tld%') AS `updated`
                                WHERE `toUpdate`.`ID`=`updated`.`ID`;






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Oct 26 '15 at 12:25









                                llange

                                6441913




                                6441913





















                                    0














                                    Generate change SQL queries (FAST)



                                    mysql -e "SELECT CONCAT( 'update ', table_name , ' set ', column_name, ' = replace(', column_name,', ''www.oldsite.com'', ''www.newsite.com'');' ) AS statement FROM information_schema.columns WHERE table_name LIKE 'wp_%'" -u root -p your_db_name_here > upgrade_script.sql



                                    Remove any garbage at the start of the file. I had some.



                                    nano upgrade_script.sql



                                    Run generated script with --force options to skip errors. (SLOW - grab a coffee if big DB)



                                    mysql -u root -p your_db_name_here --force < upgrade_script.sql






                                    share|improve this answer

























                                      0














                                      Generate change SQL queries (FAST)



                                      mysql -e "SELECT CONCAT( 'update ', table_name , ' set ', column_name, ' = replace(', column_name,', ''www.oldsite.com'', ''www.newsite.com'');' ) AS statement FROM information_schema.columns WHERE table_name LIKE 'wp_%'" -u root -p your_db_name_here > upgrade_script.sql



                                      Remove any garbage at the start of the file. I had some.



                                      nano upgrade_script.sql



                                      Run generated script with --force options to skip errors. (SLOW - grab a coffee if big DB)



                                      mysql -u root -p your_db_name_here --force < upgrade_script.sql






                                      share|improve this answer























                                        0












                                        0








                                        0






                                        Generate change SQL queries (FAST)



                                        mysql -e "SELECT CONCAT( 'update ', table_name , ' set ', column_name, ' = replace(', column_name,', ''www.oldsite.com'', ''www.newsite.com'');' ) AS statement FROM information_schema.columns WHERE table_name LIKE 'wp_%'" -u root -p your_db_name_here > upgrade_script.sql



                                        Remove any garbage at the start of the file. I had some.



                                        nano upgrade_script.sql



                                        Run generated script with --force options to skip errors. (SLOW - grab a coffee if big DB)



                                        mysql -u root -p your_db_name_here --force < upgrade_script.sql






                                        share|improve this answer












                                        Generate change SQL queries (FAST)



                                        mysql -e "SELECT CONCAT( 'update ', table_name , ' set ', column_name, ' = replace(', column_name,', ''www.oldsite.com'', ''www.newsite.com'');' ) AS statement FROM information_schema.columns WHERE table_name LIKE 'wp_%'" -u root -p your_db_name_here > upgrade_script.sql



                                        Remove any garbage at the start of the file. I had some.



                                        nano upgrade_script.sql



                                        Run generated script with --force options to skip errors. (SLOW - grab a coffee if big DB)



                                        mysql -u root -p your_db_name_here --force < upgrade_script.sql







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Nov 3 '17 at 17:44









                                        Andy

                                        650914




                                        650914





















                                            0














                                            the best you export it as sql file and open it with editor such as visual studio code and find and repalace your words.
                                            i replace in 1 gig file sql in 1 minutes for 16 word that total is 14600 word.
                                            its the best way.
                                            and after replace it save and import it again.
                                            do not forget compress it with zip for import.






                                            share|improve this answer

























                                              0














                                              the best you export it as sql file and open it with editor such as visual studio code and find and repalace your words.
                                              i replace in 1 gig file sql in 1 minutes for 16 word that total is 14600 word.
                                              its the best way.
                                              and after replace it save and import it again.
                                              do not forget compress it with zip for import.






                                              share|improve this answer























                                                0












                                                0








                                                0






                                                the best you export it as sql file and open it with editor such as visual studio code and find and repalace your words.
                                                i replace in 1 gig file sql in 1 minutes for 16 word that total is 14600 word.
                                                its the best way.
                                                and after replace it save and import it again.
                                                do not forget compress it with zip for import.






                                                share|improve this answer












                                                the best you export it as sql file and open it with editor such as visual studio code and find and repalace your words.
                                                i replace in 1 gig file sql in 1 minutes for 16 word that total is 14600 word.
                                                its the best way.
                                                and after replace it save and import it again.
                                                do not forget compress it with zip for import.







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Nov 11 at 7:15









                                                Omid Ahmadyani

                                                15116




                                                15116















                                                    protected by Community May 29 '14 at 19:25



                                                    Thank you for your interest in this question.
                                                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                    Would you like to answer one of these unanswered questions instead?



                                                    Popular posts from this blog

                                                    Use pre created SQLite database for Android project in kotlin

                                                    Darth Vader #20

                                                    Ondo