I\'d like to get a better idea of what domains my customers are using. I could easily do this in PHP by explode
ing each address and counting the domain that way
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
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;
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
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;
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,"@"));
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;