要仅替换单个字符,请REPLACE()
在MySQL中使用。让我们首先创建一个表-
mysql> create table DemoTable -> ( -> Name varchar(20) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable values('John Smitk'); mysql> insert into DemoTable values('David Miller'); mysql> insert into DemoTable values('Adam Smitk');
使用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'); 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)