MySQL Query to Count Unique Domains from Email Address field

后端 未结 7 1825
执念已碎
执念已碎 2020-12-13 02:16

I\'d like to get a better idea of what domains my customers are using. I could easily do this in PHP by explodeing each address and counting the domain that way

相关标签:
7条回答
  • 2020-12-13 02:41
    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
    
    0 讨论(0)
  • 2020-12-13 02:42

    Small tweak to Wolph's original above to shorten a bit and add nice column name and limit results in case list is long. Adjust limit to your own liking

    select substring_index(email, '@', -1) AS domain, count(*) from TABLE group by domain order by count(*) DESC limit 40;
    
    0 讨论(0)
  • 2020-12-13 02:43
    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
    
    0 讨论(0)
  • 2020-12-13 02:46

    Adding ORDER BY to WoLpH's answer makes the output more clear:

    SELECT substring_index(email, '@', -1), COUNT(*) AS MyCount
    FROM `database`.`table`
    GROUP BY substring_index(email, '@', -1)
    ORDER BY MyCount DESC;
    
    0 讨论(0)
  • 2020-12-13 02:51

    You can use this query to get Unique count of domain from table.

    SELECT substr(email,INSTR(email,"@")+1),count(substr(email,INSTR(email,"@"))) from YOUR_TABLE group by substr(email,INSTR(email,"@"));
    
    0 讨论(0)
  • 2020-12-13 02:53

    You would have to do something like this:

    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;
    
    0 讨论(0)
提交回复
热议问题