2011-02-25 59 views
2

我正在使用mysql,我想在我的数据库中找到最常见的电子邮件后缀。Sql代码列出数据库中最常见的电子邮件后缀

Example Dataset: 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 
[email protected] 

我想要的输出:

gmail.com - 3 
yahoo.com - 2 
hotmail.com - 1 
aol.com - 1 

我的第一直觉就是运行SQL命令,然后"SELECT userEmail FROM email_user_testing"运行一个循环和操作与PHP的数据,但我有一种感觉它会使用sql命令更有效率。

回答

5
SELECT 
    SUBSTR(userEmail FROM LOCATE('@', userEmail) + 1), 
    COUNT(*) 
FROM 
    email_user_testing 
GROUP BY 
    SUBSTR(userEmail FROM LOCATE('@', userEmail) + 1) 
ORDER BY 
    COUNT(*) DESC 
0
SELECT 
    count(domain) as count 
    ,domain 
FROM 
    (
    SELECT 
    REVERSE(
     LEFT(REVERSE(email),LOCATE('@',REVERSE(email)) - 1) 
    ) 
    AS domain 
    FROM table 
) 
    GROUP BY 
    domain 
    ORDER BY 
    count DESC 
3
select 
substring_index(usermail,'@',-1) as domain, 
count(*) as number 
from your_table 
group by domain 
order by number desc 
+0

+1:很好!从来不知道有关subtring_index :) – MatBailie 2011-02-25 18:56:11

0

这是我发现我的数据库中最常用的电子邮件域。

SELECT 
    SUBSTRING_INDEX(email,'@',-1) AS domain, 
    COUNT(*) as count 
FROM 
    users 
GROUP BY 
    domain 
ORDER BY 
    count desc; 

+----------------------------------------------+-------+ 
| domain          | count | 
+----------------------------------------------+-------+ 
| hotmail.com         | 51539 | 
| gmail.com         | 49470 | 
| hotmail.co.uk        | 38872 | 
| yahoo.co.uk         | 18528 | 
| yahoo.com         | 18082 | 
| live.co.uk         | 7662 | 
| googlemail.com        | 5481 | 
| btinternet.com        | 3645 | 
| aol.com          | 3281 | 
| msn.com          | 2137 | 
| live.com          | 2103 | 
| ymail.com         | 1450 | 
| wp.pl          | 1290 | 
| sky.com          | 1226 | 
| hotmail.it         | 1092 | 
| hotmail.fr         | 1063 | 
...