it-source

전자 메일 주소에서 고유 도메인 수를 계산하기 위한 MySQL 쿼리 필드

criticalcode 2023. 8. 29. 20:42
반응형

전자 메일 주소에서 고유 도메인 수를 계산하기 위한 MySQL 쿼리 필드

고객이 어떤 도메인을 사용하고 있는지 더 잘 알고 싶습니다.나는 PHP로 이것을 쉽게 할 수 있었습니다.explode각 주소를 입력하고 도메인을 그런 식으로 세는 것입니다.하지만 간단한 MySQL 쿼리만으로 이 정보를 얻을 수 있는 방법이 있는지 궁금합니다.

샘플 출력은 다음과 같습니다.

gmail.com | 3942

yahoo.com | 3852

hotmail.com | 209

여기서 첫 번째 열은 전자 메일 주소 도메인이고 두 번째 열은 해당 도메인의 주소 수입니다.

다음과 같은 작업을 수행해야 합니다.

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;

WoLpH의 답변에 ORDER BY를 추가하면 출력이 더욱 명확해집니다.

SELECT substring_index(email, '@', -1), COUNT(*) AS MyCount
FROM `database`.`table`
GROUP BY substring_index(email, '@', -1)
ORDER BY MyCount DESC;
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

위의 Wolph 원본을 작게 수정하여 약간 단축하고 목록이 긴 경우 멋진 열 이름과 제한 결과를 추가합니다.원하는 대로 제한 조정

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

이 쿼리를 사용하여 테이블에서 고유 도메인 수를 가져올 수 있습니다.

SELECT substr(email,INSTR(email,"@")+1),count(substr(email,INSTR(email,"@"))) from YOUR_TABLE group by substr(email,INSTR(email,"@"));
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

그런 건 어때요?

SELECT COUNT(DISTINCT [what you want])
FROM MyTable

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

언급URL : https://stackoverflow.com/questions/2440429/mysql-query-to-count-unique-domains-from-email-address-field

반응형