通过MySQL DB中的regex选择列的一部分

时间:2014-05-30 07:41:02

标签: mysql regex

我有一张电子邮件表,它们都像redir。IDENTIFIER @ redirthis.url

如何运行选择identifier的查询?

emails

+----+--------------------------------+
| id | email                          |
+----+--------------------------------+
|  1 | redir.A73283@redirthis.url     |
|  2 | redir.XAAX83@redirthis.url     |
|  3 | redir.A73283F3GH@redirthis.url |
+----+--------------------------------+

预期/期望的结果

+----+------------+
| id | email      |
+----+------------+
|  1 | A73283     |
|  2 | XAAX83     |
|  3 | A73283F3GH |
+----+------------+

1 个答案:

答案 0 :(得分:1)

如果模式相同,您可以使用substring_index

mysql> select substring_index(substring_index('redir.A73283@redirthis.url','@',1),'.',-1);
+-----------------------------------------------------------------------------+
| substring_index(substring_index('redir.A73283@redirthis.url','@',1),'.',-1) |
+-----------------------------------------------------------------------------+
| A73283                                                                      |
+-----------------------------------------------------------------------------+

所以查询将是

select 
id,
substring_index(substring_index(email,'@',1),'.',-1) as email
from emails