How to replace data in query results in MySQL?
In MySQL, you can use the REPLACE function to replace data in query results. The syntax of the REPLACE function is as follows:
REPLACE(str, find_string, replace_with)
In the given scenario, ‘str’ is the string where the replace operation is to be performed, ‘find_string’ is the substring to be replaced, and ‘replace_with’ is the new string used for replacement.
Here is an example:
Assuming there is a table called students, consisting of columns id and name, now we want to query all student names and replace “王” with “李”.
SELECT id, REPLACE(name, '王', '李') AS new_name
FROM students;
This will yield a search result that includes the new name.