MySQL 中是否存在類似 substr_replace 的函式?


為此,請使用 MySQL 的 INSERT() 函式。INSERT(str, pos, len, newstr) 返回字串 str,其中從位置 pos 開始、len 個字元長度的子字串由字串 newstr 替換。如果 pos 不在字串長度範圍內,則返回原始字串。

如果 len 不在剩餘字串的長度範圍內,則會替換從位置 pos 開始的剩餘字串。如果任何引數為 NULL,則返回 NULL。

我們首先建立一個表 −

mysql> create table DemoTable
(
   Password varchar(50)
);
Query OK, 0 rows affected (0.51 sec)

使用 insert 命令在表中插入一些記錄 −

mysql> insert into DemoTable values('76367_____8793443');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('12345_____9899984322');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('99999_____4747747432');
Query OK, 1 row affected (0.22 sec)

使用 select 語句顯示錶中的所有記錄 −

mysql> select *from DemoTable;

這將生成以下輸出 −

+----------------------+
| Password             |
+----------------------+
| 76367_____8793443    |
| 12345_____9899984322 |
| 99999_____4747747432 |
+----------------------+
3 rows in set (0.00 sec)

以下是針對 MySQL 中 substr_replace 實現 INSERT() 函式的查詢 −

mysql> select insert(Password,5,length('PPPPPP'),'PPPPPP') from DemoTable;

這將生成以下輸出 −

+----------------------------------------------+
| insert(Password,5,length('PPPPPP'),'PPPPPP') |
+----------------------------------------------+
| 7636PPPPPP8793443                            |
| 1234PPPPPP9899984322                         |
| 9999PPPPPP4747747432                         |
+----------------------------------------------+
3 rows in set (0.00 sec)

更新於: 04-Oct-2019

136 次瀏覽

開啟你的 職業生涯

完成課程取得認證

開始
廣告
© . All rights reserved.