MySQL查询从电子邮件地址字段计算唯一域

时间:2010-03-13 23:13:07

标签: email mysql count dns

我想更好地了解客户使用的域名。我可以通过explode每个地址并以这种方式计算域来轻松地在PHP中执行此操作。但是我想知道是否有办法通过简单的MySQL查询来获取这些信息?

这是示例输出的样子:

gmail.com | 3942

yahoo.com | 3852

hotmail.com | 209

...依此类推,第一列是电子邮件地址域,第二列是该域的地址数。

7 个答案:

答案 0 :(得分:77)

你必须做这样的事情:

SELECT substring_index(email, '@', -1) domain, COUNT(*) email_count
FROM table
GROUP BY substring_index(email, '@', -1)

-- If you want to sort as well:
ORDER BY email_count DESC, domain;

答案 1 :(得分:2)

将ORDER BY添加到WoLpH的答案会使输出更清晰:

SELECT substring_index(email, '@', -1), COUNT(*) AS MyCount
FROM `database`.`table`
GROUP BY substring_index(email, '@', -1)
ORDER BY MyCount DESC;

答案 2 :(得分:1)

对Wolph上面原版的小调整以缩短一点并添加好的列名并限制结果列表很长。根据自己的喜好调整限制

select substring_index(email, '@', -1) AS domain, count(*) from TABLE group by domain order by count(*) DESC limit 40;

答案 3 :(得分:0)

select distinct SUBSTRING(Email, CHARINDEX('@', Email) + 1,LEN(Email) - CHARINDEX ('@', Email)), Count(*) from Tbl_name
Group by SUBSTRING(Email, CHARINDEX('@', Email) + 1,LEN(Email) - CHARINDEX ('@', Email))
order by Count(*) desc

答案 4 :(得分:0)

您可以使用此查询从表中获取唯一的域数。

SELECT substr(email,INSTR(email,"@")+1),count(substr(email,INSTR(email,"@"))) from YOUR_TABLE group by substr(email,INSTR(email,"@"));

答案 5 :(得分:0)

SELECT 
    substring_index(email_address, '@', -1) AS Domain 
   ,COUNT(*) AS MyCount
FROM 
    database_name.table_name
GROUP BY 
    substring_index(email_address, '@', -1)
ORDER BY
    MyCount DESC

答案 6 :(得分:-1)

这样的东西
SELECT COUNT(DISTINCT [what you want])
FROM MyTable

COUNT(DISTINCT expr,[expr...])