新特性解讀 | MySQL 8.0 正則替換

原創: 楊濤濤mysql

MySQL 一直以來都支持正則匹配,不過對於正則替換則一直到MySQL 8.0 才支持。對於這類場景,之前要麼在MySQL端處理,要麼把數據拿出來在應用端處理。git

好比我想把表y1的列str1的出現第3個action的子 串替換成dble,怎麼實現?sql

1. 本身寫SQL層的存儲函數。代碼以下寫死了3個,沒有優化,僅僅做爲演示,MySQL 裏很是不建議寫這樣的函數。shell

mysql
DELIMITER $$
USE `ytt`$$
DROP FUNCTION IF EXISTS `func_instr_simple_ytt`$$
CREATE DEFINER=`root`@`localhost` FUNCTION `func_instr_simple_ytt`(
f_str VARCHAR(1000), -- Parameter 1
f_substr VARCHAR(100), -- Parameter 2
f_replace_str varchar(100),
f_times int -- times counter.only support 3.
) RETURNS varchar(1000)
BEGIN
declare v_result varchar(1000) default 'ytt'; -- result.
declare v_substr_len int default 0; -- search string length.

set f_times = 3; -- only support 3.
set v_substr_len = length(f_substr);
select instr(f_str,f_substr) into @p1; -- First real position .
select instr(substr(f_str,@p1+v_substr_len),f_substr) into @p2; Secondary virtual position.
select instr(substr(f_str,@p2+ @p1 +2*v_substr_len - 1),f_substr) into @p3; -- Third virtual position.
if @p1 > 0 && @p2 > 0 && @p3 > 0 then -- Fine.
select
concat(substr(f_str,1,@p1 + @p2 + @p3 + (f_times - 1) * v_substr_len - f_times)
,f_replace_str,
substr(f_str,@p1 + @p2 + @p3 + f_times * v_substr_len-2)) into v_result;
else
set v_result = f_str; -- Never changed.
end if;
-- Purge all session variables.
set @p1 = null;
set @p2 = null;
set @p3 = null;
return v_result;

end;
$$
DELIMITER ;

-- 調用函數來更新:
mysql> update y1 set str1 = func_instr_simple_ytt(str1,'action','dble',3);
Query OK, 20 rows affected (0.12 sec)
Rows matched: 20 Changed: 20 Warnings: 0

2. 導出來用sed之類的工具替換掉在導入,步驟以下:(推薦使用)session

1)導出表y1的記錄。函數

mysql
mysql> select * from y1 into outfile '/var/lib/mysql-files/y1.csv';
Query OK, 20 rows affected (0.00 sec)

2)用sed替換導出來的數據。工具

shell
root@ytt-Aspire-V5-471G:/var/lib/mysql-files# sed -i 's/action/dble/3' y1.csv

3)再次導入處理好的數據,完成。性能

mysql
mysql> truncate y1;
Query OK, 0 rows affected (0.99 sec)

mysql> load data infile '/var/lib/mysql-files/y1.csv' into table y1;
Query OK, 20 rows affected (0.14 sec)
Records: 20 Deleted: 0 Skipped: 0 Warnings: 0

以上兩種仍是推薦導出來處理好了再從新導入,性能來的高些,並且還不用本身費勁寫函數代碼。優化

那MySQL 8.0 對於以上的場景實現就很是簡單了,一個函數就搞定了。code

mysql
mysql> update y1 set str1 = regexp_replace(str1,'action','dble',1,3) ;
Query OK, 20 rows affected (0.13 sec)
Rows matched: 20 Changed: 20 Warnings: 0

還有一個regexp_instr 也很是有用,特別是這種特指出現第幾回的場景。好比定義 SESSION 變量@a。

mysql
mysql> set @a = 'aa bb cc ee fi lucy 1 1 1 b s 2 3 4 5 2 3 5 561 19 10 10 20 30 10 40';
Query OK, 0 rows affected (0.04 sec)

拿到至少兩次的數字出現的第二次子串的位置。

mysql
mysql> select regexp_instr(@a,'[:digit:]{2,}',1,2);
+--------------------------------------+
| regexp_instr(@a,'[:digit:]{2,}',1,2) |
+--------------------------------------+
| 50 |
+--------------------------------------+
1 row in set (0.00 sec)

那咱們在看看對多字節字符支持如何。

mysql
mysql> set @a = '中國 美國 俄羅斯 日本 中國 北京 上海 深圳 廣州 北京 上海 武漢 東莞 北京 青島 北京';
Query OK, 0 rows affected (0.00 sec)

mysql> select regexp_instr(@a,'北京',1,1);
+-------------------------------+
| regexp_instr(@a,'北京',1,1) |
+-------------------------------+
| 17 |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select regexp_instr(@a,'北京',1,2);
+-------------------------------+
| regexp_instr(@a,'北京',1,2) |
+-------------------------------+
| 29 |
+-------------------------------+
1 row in set (0.00 sec)

mysql> select regexp_instr(@a,'北京',1,3);
+-------------------------------+
| regexp_instr(@a,'北京',1,3) |
+-------------------------------+
| 41 |
+-------------------------------+
1 row in set (0.00 sec)

那總結下, 這裏我提到了 MySQL 8.0 的兩個最有用的正則匹配函數 regexp_replace 和 regexp_instr。針對之前相似的場景算是有一個完美的解決方案。

相關文章
相關標籤/搜索