如何在MySQL中仅替换字符串中的第一个重复值
为此,您可以使用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 −> );
借助insert命令将一些记录插入表中-
mysql> insert into demo26 values('This is my first MySQL query. This is the first tutorial. I am learning for the first time.');使用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 ) ; 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)
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短