如何计算按域分组的电子邮件地址?

时间:2014-07-15 07:26:22

标签: mysql

考虑到这样的电子邮件地址数据,我需要显示每个域中的用户数。:

+----------------------+
| email                |
+----------------------+
| richardg07@gmail.com |
| anolimous@yahoo.com  |
| binnerts@yahoo.com   |
| retsel@yahoo.com     |
| Val@gmail.com        |
| mayaman@yahoo.com    |
| marylaine@yahoo.com  |
| richardg07@gmail.com |
| shaider07@lycos.com  |
| yayabelles@yahoo.com |
+----------------------+

我想要这样的输出:

+----------------------+-------------+
| Email Acount         | Total Users |
+----------------------+-------------+
| gmail.com            | 3           |
| lycos.com            | 1           |
| yahoo.com            | 6           |
+----------------------+-------------+

我应该如何在MySQL中做到这一点?

1 个答案:

答案 0 :(得分:3)

SELECT SUBSTRING_INDEX(email, '@', -1) AS `Email Acount`, COUNT(*) AS `Total Users`
FROM YourTable
GROUP BY `Email Acount`

MySQL SUBSTRING_INDEX函数documentation

DEMO