如何在 MySQL 中仅替换字符串中的第一个重复值
mysqlmysqli database
为此,您可以使用 REGEXP_REPLACE()。假设我们的字符串是 −
This is my first MySQL query. This is the first tutorial. I am learning for the first time.
我们只需要替换特定单词的第一次出现,假设为"first"。输出应该是 −
This is my second MySQL query. This is the first tutorial. I am learning for the first time.
让我们创建一个表 −
mysql> create table demo26 −> ( −> value text −> ); Query OK, 0 rows affected (2.04 sec)
使用 insert 命令向表中插入一些记录 −
mysql> insert into demo26 values('This is my first MySQL query. This is the first tutorial. I am learning for the first time.');
Query OK, 1 row affected (0.10 sec)
使用 select 语句显示表中的记录 −
mysql> select *from demo26;
这将产生以下输出 −
+---------------------------------------------------------------------------------------------+ | value | +---------------------------------------------------------------------------------------------+ | This is my first MySQL query. This is the first tutorial. I am learning for the first time. | +---------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
以下是仅替换第一次出现的查询 −
mysql> update demo26 −> set value = REGEXP_REPLACE(value, 'first', 'second', 1, 1 ) ; Query OK, 1 row affected (0.19 sec) Rows matched: 1 Changed: 1 Warnings: 0
使用 select 语句显示表中的记录 −
mysql> select *from demo26;
这将产生以下输出 −
+----------------------------------------------------------------------------------------------+ | value | +----------------------------------------------------------------------------------------------+ | This is my second MySQL query. This is the first tutorial. I am learning for the first time. | +----------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/
打印
下一节:如何在 MySQL 中根据特定值选择除较低值记录之外的记录? ❯❮ 上一节:Springboot + JSP + Spring Security:配置DataSource失败。如何在MySQL中配置DataSource?

