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.