如何在MySQL表中替换字符?
要仅替换单个字符,请在MySQL中使用REPLACE()。 首先让我们创建一个表 –
mysql> create table DemoTable
-> (
-> Name varchar(20)
-> );
Query OK, 0 rows affected (0.69 sec)
使用insert命令将一些记录插入表中 –
mysql> insert into DemoTable values('John Smitk');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('Adam Smitk');
Query OK, 1 row affected (0.11 sec)
使用select语句显示表中的所有记录 –
mysql> select * from DemoTable;
这将产生以下输出 –
+--------------+
| Name |
+--------------+
| John Smitk |
| David Miller |
| Adam Smitk |
+--------------+
3 rows in set (0.00 sec)
以下是替换字符的查询 –
mysql> update DemoTable
-> set Name=replace(Name,'k','h');
Query OK, 2 rows affected (0.13 sec)
Rows matched: 3 Changed: 2 Warnings: 0
让我们再次检查表记录 –
mysql> select * from DemoTable;
这将产生以下输出 –
+--------------+
| Name |
+--------------+
| John Smith |
| David Miller |
| Adam Smith |
+--------------+
3 rows in set (0.00 sec)
阅读更多:MySQL 教程
极客教程