Home >Database >Mysql Tutorial >How to Replace a String within URLs in MySQL?

How to Replace a String within URLs in MySQL?

Susan Sarandon
Susan SarandonOriginal
2025-01-03 04:53:40775browse

How to Replace a String within URLs in MySQL?

MySQL String Replacement Solution for URL Modification

A database table contains a column of URLs with a pattern like "http://www.example.com/articles/updates/XXX," and the need is to replace the term "updates" with "news" in these URLs.

To achieve this task through a MySQL script, the following query can be executed:

UPDATE your_table
SET your_field = REPLACE(your_field, 'articles/updates/', 'articles/news/')
WHERE your_field LIKE '%articles/updates/%'

Here, "your_table" represents the relevant table, "your_field" indicates the URL column, and the LIKE clause ensures that only URLs containing "updates" are updated.

As a result, URLs like "http://www.example.com/articles/updates/43" will be transformed into "http://www.example.com/articles/news/43."

For further information on text replacement in MySQL, refer to the "Find and Replace Text" article at http://www.electrictoolbox.com/mysql-find-replace-text/.

The above is the detailed content of How to Replace a String within URLs in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn