Signup/Sign In
SEPTEMBER 1, 2022

How to Replace a String with MySQL Query

Technology #string#howto#mysql

    Did you switch your website from HTTP to HTTPS? Now, you do have a lot of old links and images that are still pointing to the HTTP version. If you start replacing each and every tag, you would probably need 100+ employees working on it.

    Instead of doing this, you can replace all of it with just a single SQL statement. Let's get to know How to replace a string with a MySQL Query and What's the syntax of this MySQL Query.

    Replace a String with a MySQL Query

    Syntax

    UPDATE table set column1=REPLACE(column1,'SEARCH','REPLACE');

    "UPDATE table set" is a MySQL command. We have to pass the column name in which the keyword is to be replaced. And a keyword that is to be replaced and another keyword to replace.

    Let's take an example -

    You are updating all links from HTTP to HTTPS in Wordpress then you would ned to write a SQL statement -

    UPDATE wp_posts set post_content = REPLACE(post_content, 'http://www.studytonight.com','https://www.studytonight.com');

    I just updated more than 10,000 posts records by replacing the old links with new HTTPS Version links.

    How to open a MySQL Prompt

    • Open your System's Terminal
    • If you are using WordPress then find the database values in wp-config.php
    • Replace the values in the following Statement
    • Paste in the SQL Statement
    mysql -uUser -pPassword -hHost databasename

    Proficient in Java, Python, and web development; has a knack for writing clearly about complex topics. Dedicated to giving readers the tools they need to learn more about computer science and technology.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS