Technology3 min read

How to Replace a String with MySQL Query

Abhishek Ahlawat

By Abhishek Ahlawat โ€” SEPTEMBER 1, 2022

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#string#howto#mysql

Summarize this article using AI

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

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops โ€” put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next