Computer >> Computer tutorials >  >> Programming >> MySQL

How to mask user email addresses with a different domain in MySQL?


Let us first create a table −

mysql> create table DemoTable1345
   -> (
   -> UserEmailAddress text
   -> );
Query OK, 0 rows affected (0.42 sec)

Insert some records in the table using insert command. We have inserted email address here −

mysql> insert into DemoTable1345 values('[email protected]');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1345 values('[email protected]');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable1345 values('[email protected]');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable1345 values('[email protected]');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1345;

This will produce the following output −

+------------------------+
| UserEmailAddress       |
+------------------------+
| [email protected]     |
| [email protected]       |
| [email protected] |
| [email protected]          |
+------------------------+
4 rows in set (0.00 sec)

Following is the query to mask user email addresses with different domains in MySQL −

mysql> update DemoTable1345 set UserEmailAddress=replace(UserEmailAddress, '@gmail.com','@amz.com');
Query OK, 4 rows affected (0.18 sec)
Rows matched: 4 Changed: 4 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1345;

This will produce the following output −

+----------------------+
| UserEmailAddress     |
+----------------------+
| [email protected]     |
| [email protected]       |
| [email protected] |
| [email protected]          |
+----------------------+
4 rows in set (0.00 sec)