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

Select all email addresses beginning with 5 numeric characters (regular expression) in MySQL


To get the email addresses beginning with 5 numeric characters, the optional solution is to use REGEXP −

select *from yourTableName where yourColumnName regexp "^[0-9]{5}";

Let us first create a table −

mysql> create table DemoTable
(
   UserEmailAddress varchar(100)
);
Query OK, 0 rows affected (0.76 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values('[email protected]');
Query OK, 1 row affected (0.20 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

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

Following is the query to select all email addresses beginning with 5 numeric characters −

mysql> select *from DemoTable where UserEmailAddress regexp "^[0-9]{5}";

This will produce the following output −

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