Home >Database >Mysql Tutorial >How Can I Update Parts of Strings in a MySQL Table?
Updating a Portion of a String using MySQL
In MySQL, updating a specific portion of a string can be achieved through the REPLACE() function. This function allows you to replace a substring with another substring within a given string.
Example:
Let's say we have a table with the following values in a column named "field":
id | field |
---|---|
1 | something/string |
2 | something/stringlookhere |
3 | something/string/etcetera |
We want to update all the records where "string" appears in the "field" column and replace it with "anothervalue."
To do this, we can use the following MySQL query:
UPDATE table SET field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';
Breakdown of the Query:
The REPLACE() function takes three arguments:
After executing this query, the values in the "field" column would be updated as follows:
id | field |
---|---|
1 | something/anothervalue |
2 | something/anothervaluelookhere |
3 | something/string/etcetera |
The above is the detailed content of How Can I Update Parts of Strings in a MySQL Table?. For more information, please follow other related articles on the PHP Chinese website!