【问题标题】:Postgres Get Count of Email DomainsPostgres 获取电子邮件域的数量
【发布时间】:2017-06-14 22:38:49
【问题描述】:

如果我的数据库中有用户电子邮件条目,我想查看有多少用户拥有域 @gmail.com 以及有多少用户拥有域 @yahoo.com 等等。最好的方法是什么?

现在我正在逐一手动运行查询:

select email from "user" where email ilike '%@gmail.com';

【问题讨论】:

  • 可以先做一个substr操作,然后equate,会快一点。此外,您可以在电子邮件列上创建索引以进一步加快查询速度

标签: sql postgresql


【解决方案1】:

我的第一个想法是提取域和分组:

select substring(email from '@(.*)$') as domain, count(*)
from "user"
group by domain

【讨论】:

    【解决方案2】:

    在这种情况下可以使用函数split_part

    select split_part(email, '@', 2) "domain", count(*)
    from emails 
    group by "domain";
    

    PostgreSQL split_part fiddle

    【讨论】:

      【解决方案3】:

      这是一种不同的方法:

      select dm.domain, count(*) as total
      from (
               select lower(right(email, char_length(email) - position('@' in email))) as domain
               from "user"
               group by  domain, email)dm
      group by dm.domain
      order by total desc
      

      【讨论】:

        【解决方案4】:
        select substring(email from '@(.*)$') as domain, count(*)
        from  pytest.emps
        group by domain
        
        SELECT distinct(split_part (email,'@',2) ) as domain_name FROM pytest.emps 
        
        SELECT split_part (email,'@',2) as domain_name FROM pytest.emps group by domain_name
        

        【讨论】:

          猜你喜欢
          • 2013-09-29
          • 2019-03-11
          • 1970-01-01
          • 2013-02-13
          • 1970-01-01
          • 2019-10-12
          • 1970-01-01
          • 1970-01-01
          • 2018-08-25
          相关资源
          最近更新 更多