如何在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
−> );
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)
以下是仅替换第1次出现的查询 –
mysql> update demo26
−> set value = REGEXP_REPLACE(value,'first','second',1,1);
Query OK,1 row affected(0.19 sec)
匹配的行:1 已更改:1 警告: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 教程
极客教程