首页 > 解决方案 > 如何在 SQL 中使用选择查询来查找仅包含 6 个随机数的电子邮件地址列表?

问题描述

我目前正在寻找一种使用 SQL 查询的方法,以便在我们的数据库中查找所有电子邮件地址的列表,其中只有 6 个随机数,然后是“@gmail.com”。

例子:

email

----------

123456@gmail.com
324522@gmail.com

这是我尝试过的:

select email 
from customers
where email Not like '%^[0-9]%'

当我运行它时,所有电子邮件都会出现,即使是那些没有任何数字的电子邮件。

select email,
SPLIT_PART(email, '@',1) as username, 
SPLIT_PART(email, '@',2) as domain,
(case when username not like '%^[0-9]%' then 'Incorrect' else 'Correct' End) as format
from customers
where domain = 'gmail.com'
and format = 'Correct' 

我也尝试过,对于所有电子邮件,即使它们中有数字,它们也会显示为不正确。

似乎无法识别列中的数字,我不确定如何解决。列格式为 Varchar

标签: sqlamazon-redshiftmode-analytics

解决方案


我与 Mode Analytics 交谈,结果发现我的数据库是 Redshift,这就是我能够完成这项工作的方式:

select email
from customers
where email similar to '[0-9]{6}@gmail.com'

感谢大家的帮助!


推荐阅读