JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

How to replace a string using MySQL query

Author:JIYIK Last Updated:2025/04/26 Views:

This is a very naive post, and the only reason for writing it is so that I won't forget the syntax and have to Google MySQL REPLACEthe order of the arguments to the _ function again. And then when I try to look it up, I almost always end up on the documentation page for MySQL's other REPLACE_ command... which is used to update rows in a table using INSERTthe same syntax as the _ statement. Your confusion, MySQL!

What I'd like to do is search and replace in my database with a simple query WordPress- for example, when we switched HTG to HTTPS , we had a ton of old links and images pointing to the HTTP version. So we had to <img src=”http://www.jiyik.com/”>update all of our links to <img src=”https://www.jiyik.com”>, and with nearly 20,000 post records in our database, that would have required hiring a hundred interns. Or... one SQL statement.

So now, and hopefully this is the last time I have to look it up, use the MySQL query replacement string syntax:

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

For the purposes of our example, WordPresswe’ll update all of our links and images to HTTPS in , ending up with:

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

This saves us from hiring 100 people to manually replace all those links. They deserve a raise.

Are you new here? If you don’t know how to open a MySQL prompt, then you probably shouldn’t be reading this article. But just in case, you can open a terminal and connect to your database using the values ​​from wp-config.php (assuming you’re using WordPress), then paste in your SQL statements.

mysql -uUser -pPassword -hHost databasename

Did I mention that we should probably back up our database before running this WordPress? And we should definitely test it in a development or QA environment? Otherwise you'll break everything.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Two ways to install mysql-5.5.47 on Linux system and manage mysql

Publish Date:2025/04/26 Views:140 Category:MySQL

We know that there are generally two ways to install software on the Linux system. One is to use rpm or yum to install, which is convenient and fast; the other is to use the compiled source package. Although this method is more troublesome

Mysql master-slave replication simple configuration

Publish Date:2025/04/26 Views:120 Category:MySQL

I'm learning about MySQL master-slave replication recently and want to apply it to a project at work. Since I'm new to it, I don't understand it very well, so I can only share how to make a simple configuration. At the beginning, we configu

MySQL stored procedure details

Publish Date:2025/04/26 Views:163 Category:MySQL

A stored procedure can be thought of as encapsulating a SQL statement that we need to process specially into a function. When needed, we only need to call this function to achieve the desired operation. This process can be called a stored p

How many of these MySQL statement tags have you used?

Publish Date:2025/04/26 Views:122 Category:MySQL

In the article "A Peek into MySQL Stored Procedure Details" , we briefly introduced the use of stored procedures. The syntax for creating stored procedures includes BEGIN...END. In addition to BEGIN...END, the following statement tags can b

Back up the MySQL database to a file

Publish Date:2025/04/26 Views:166 Category:MySQL

Backing up your database is a very important system administration task that should usually cron be run from a job at scheduled intervals. We will use mysqldump the dump utility that comes with mysql to dump the contents of the database to

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial