如何使用MySQL更改字符串的一部分(@后面的域名)?
首先,让我们创建一个表 –
mysql> create table DemoTable
-> (
-> EmailId varchar(30)
-> );
Query OK, 0 rows affected (0.53 sec)
使用插入命令在表中插入一些记录 –
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)
阅读更多:MySQL 教程
极客教程