在 MySQL 中选择以 5 个数字字符开头的所有电子邮件地址(正则表达式)


要获取以 5 个数字字符开头的电子邮件地址,可选的解决方案是使用 REGEXP −

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

我们首先创建一个表 −

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

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

mysql> insert into DemoTable values('6574John@gmail.com');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Carol23456@gmail.com');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('98989Chris_45678@gmail.com');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable values('Mike12@gmail.com');
Query OK, 1 row affected (0.43 sec)
mysql> insert into DemoTable values('56453Adam@gmail.com');
Query OK, 1 row affected (0.20 sec)

使用 select 语句显示表中的所有记录 −

mysql> select *from DemoTable;

这将产生以下输出 −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 6574John@gmail.com         |
| Carol23456@gmail.com       |
| 98989Chris_45678@gmail.com |
| Mike12@gmail.com           |
| 56453Adam@gmail.com        |
+----------------------------+
5 rows in set (0.00 sec)

以下是选择以 5 个数字字符开头的所有电子邮件地址的查询 −

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

这将产生以下输出 −

+----------------------------+
| UserEmailAddress           |
+----------------------------+
| 98989Chris_45678@gmail.com |
| 56453Adam@gmail.com        |
+----------------------------+
2 rows in set (0.00 sec)

更新时间: 2019 年 10 月 7 日

178 次浏览

开启你的 职业生涯

完成课程取得认证

开始
广告
© . All rights reserved.