Home >Database >Mysql Tutorial >How to Perform Text Search and Replace in MySQL?
MySQL Text Search and Replace
Performing a text search and replace operation on a specific field in a MySQL table is a common task in database management. This article provides a comprehensive solution for achieving this functionality.
To search for a particular text (e.g., 'foo') and replace it with another (e.g., 'bar') within a specific field (e.g., 'field'), you can use the following MySQL query:
UPDATE table_name SET field = REPLACE(field, 'foo', 'bar') WHERE INSTR(field, 'foo') > 0;
Let's break down this query:
Note: The INSTR() function is MySQL-specific, and other RDBMS may have different functions for text searching. Consult your RDBMS's documentation for alternatives.
By executing this query, all records in the 'table_name' with the 'foo' text in the 'field' will have 'foo' replaced with 'bar'. For example, a record with a field value of 'hello foo' will become 'hello bar'.
The above is the detailed content of How to Perform Text Search and Replace in MySQL?. For more information, please follow other related articles on the PHP Chinese website!