我有一个用户信息表,我想从中获取一份列出最常见域名的报告。我知道我需要使用count和group by,但是我不知道如何只用字符串的一部分来组合,来自'@'符号。有什么建议吗?
id email name etc..
---------------------------------------------
1 username@domain.com User Userson blah
答案 0 :(得分:1)
SELECT
SUBSTRING(email FROM LOCATE('@', email)) AS domain
COUNT(*) AS numusers
FROM tbl
GROUP BY domain
ORDER BY numusers DESC
以上内容将域名列为@example.com
。要取消使用@
代替:
SUBSTRING(email FROM LOCATE('@', email)+1) AS domain
答案 1 :(得分:1)
SUBSTRING_INDEX在这里可能很有用:
select
substring_index(email,'@',-1) as domain
,count(*) as userCount
from your_table
group by domain
order by usercount desc;