如何在 MySQL 表中替换字符?
mysqlmysqli database更新于 2024/8/14 1:16:17
若要仅替换单个字符,请在 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 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/
打印
下一节:修复 Java MySQL 连接中的连接错误,将连接器设置为类路径? ❯❮ 上一节:如何实现 MySQL ORDER BY x where (x=col3 if col3!=null, else x=col2)?

