如何在 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)
使用插入命令向表中插入一些記錄 -
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)
廣告