【问题标题】:select query using count使用计数选择查询
【发布时间】:2022-01-02 15:26:08
【问题描述】:

我想统计拥有 gmail 或 yahoo 帐户的员工人数 结果应该是

email,         count
gmail.com      3
yahoo.com      2

已经尝试过了

select count(emailid) 
from employee 
where emailid IN (select emailid from employee 
             WHERE emailid like '%@gmail.com' 
                  or select emailid from employee WHERE emailid like '%@yahoo.com')

【问题讨论】:

  • 到目前为止你做了什么?你在哪里卡住了?
  • 您使用的是哪个 dbms?
  • 从 emailid IN 的员工中选择 count(emailid)(从员工那里选择 emailid,其中 emailid 像 '%@gmail.com' 或从员工那里选择 emailid,比如 '%@yahoo.com')
  • 请标记您的数据库

标签: mysql sql select count


【解决方案1】:

如果您只需要计算 Gmail 和雅虎的电子邮件,也许一个更简单的解决方案应该是:

select count(*) as count,case when emailid like'%gmail%' then 'gmail.com' 
                              when emailid like'%yahoo%' then 'yahoo.com' end as email 
from employee
group by email;

演示:https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/173

或者,如果您只有使用 yahoo 或 Gmail 的电子邮件,请检查:

select count(*) as count,case when emailid like'%gmail%' 
                              then 'gmail.com' 
                              else 'yahoo.com' end as email 
from employee
group by email;

演示:https://www.db-fiddle.com/f/7yUJcuMJPncBBnrExKbzYz/172

结果:

count email
3     gmail.com
2     yahoo.com

【讨论】:

    【解决方案2】:
    select SUBSTRING_INDEX(Email,'@',-1), count(*) Count
    from employee 
    where Email like '%gmail.com' or  email like '%yahoo.com'
    group by  SUBSTRING_INDEX(Email,'@',-1)
    

    【讨论】:

      【解决方案3】:

      试试这个

      SELECT Substring (EmailId, Charindex( '@', EmailId ) + 1, Len(EmailId) )  , Count( EmpID)
      FROM Emp
      group by  Substring (EmailId, Charindex( '@', EmailId ) + 1, Len(EmailId) )
      

      使用函数Substring获取邮件后缀,然后按邮件后缀分组

      如果您有 gmail 和 yahoo 以外的电子邮件,并且您只想要提到的电子邮件,请使用 where 子句

      SELECT Substring (EmailId, Charindex( '@', EmailId ) + 1, Len(EmailId) )  , Count( EmpID)
      FROM Emp
      WHERE Substring (EmailId, Charindex( '@', EmailId ) + 1, Len(EmailId)) IN ('gmail.com','yahoo.com')
      group by  Substring (EmailId, Charindex( '@', EmailId ) + 1, Len(EmailId) )
      

      【讨论】:

        猜你喜欢
        • 2016-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2012-05-05
        • 1970-01-01
        相关资源
        最近更新 更多