Home > Database > Mysql Tutorial > body text

Find and replace text in an entire table using MySQL?

王林
Release: 2023-08-24 21:17:02
forward
804 people have browsed it

使用 MySQL 查找并替换整个表中的文本?

Text can be found and replaced with the help of replace() function. its explanation is With the help of the following steps -

First, create a table with the help of create command as shown below -

mysql> CREATE table FindAndReplaceDemo
-> (
-> FirstName varchar(200)
-> );
Query OK, 0 rows affected (0.43 sec)
Copy after login

After creating the above table, insert the records with the help of insert command. Given below-

mysql> INSERT into FindAndReplaceDemo values('john');
Query OK, 1 row affected (0.15 sec)

mysql> INSERT into FindAndReplaceDemo values('smith');
Query OK, 1 row affected (0.17 sec)

mysql> INSERT into FindAndReplaceDemo values('Bob');
Query OK, 1 row affected (0.12 sec)

mysql> INSERT into FindAndReplaceDemo values('carol');
Query OK, 1 row affected (0.18 sec)
Copy after login

With the help of select statement all the records can be displayed as shown below-

mysql> SELECT * from FindAndReplaceDemo;
Copy after login
Copy after login

The following is the output obtained

+-----------+
| FirstName |
+-----------+
| john      |
| smith     |
| Bob       |
| carol     |
+-----------+
4 rows in set (0.00 sec)
Copy after login

Now, with the help of substitution function , the name Carol was replaced with Taylor. The syntax is Given below -

UPDATE yourTableName SET column_name= replace(column_name, 'Old_Value', 'New_Value');
Copy after login

The query using the above syntax is as follows -

mysql> UPDATE FindAndReplaceDemo SET FirstName = replace(FirstName, 'carol', 'Taylor');
Query OK, 1 row affected (0.14 sec)
Rows matched: 4 Changed: 1 Warnings: 0
Copy after login

The contents of the table can be viewed again with the help of the SELECT statement. it's a given Below -

mysql> SELECT * from FindAndReplaceDemo;
Copy after login
Copy after login

The following is the output obtained

+-----------+
| FirstName |
+-----------+
| john      |
| smith     |
| Bob       |
| Taylor    |
+-----------+
4 rows in set (0.00 sec)
Copy after login

As can be seen from the above output, Carol is replaced with Taylor.

The above is the detailed content of Find and replace text in an entire table using MySQL?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!