在 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('[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)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+----------------------------+ | UserEmailAddress | +----------------------------+ | [email protected] | | [email protected] | | [email protected] | | [email protected] | | [email protected] | +----------------------------+ 5 rows in set (0.00 sec)
以下是选择以 5 个数字字符开头的所有电子邮件地址的查询 −
mysql> select *from DemoTable where UserEmailAddress regexp "^[0-9]{5}";
这将产生以下输出 −
+----------------------------+ | UserEmailAddress | +----------------------------+ | [email protected] | | [email protected] | +----------------------------+ 2 rows in set (0.00 sec)
广告