如何使用 MySQL 替换字符串(@ 后面的域名)的一部分?


我们首先创建一个表 -

mysql> create table DemoTable
   -> (
   -> EmailId varchar(30)
   -> );
Query OK, 0 rows affected (0.53 sec)

使用 insert 命令在表中插入一些记录 -

mysql> insert into DemoTable values('John123@example.com');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('John123@gmail.com');
Query OK, 1 row affected (0.26 sec)
mysql> insert into DemoTable values('John123@yahoo.com');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('John123@example.com');
Query OK, 1 row affected (0.10 sec)

使用 select 语句从表中显示所有记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+---------------------+
| EmailId             |
+---------------------+
| John123@example.com |
| John123@gmail.com   |
| John123@yahoo.com   |
| John123@example.com |
+---------------------+
4 rows in set (0.00 sec)

以下是替换字符串一部分的查询 -

mysql> update DemoTable
   -> set EmailId=replace(EmailId,'John123@example.com','John123@gmail.com');
Query OK, 2 rows affected (0.16 sec)
Rows matched: 4 Changed: 2 Warnings: 0

让我们再次检查表记录 -

mysql> select *from DemoTable;

这将产生以下输出 -

+-------------------+
| EmailId           |
+-------------------+
| John123@gmail.com |
| John123@gmail.com |
| John123@yahoo.com |
| John123@gmail.com |
+-------------------+
4 rows in set (0.00 sec)

更新时间: 2019 年 12 月 12 日

326 次浏览

开启你的职业

完成课程并获得认证

开始行动
广告
© . All rights reserved.