2017-10-23 4 views
0

私は電子メールのテーブルを持っています。同じ電子メールの数ユニークな電子メールのドメイン数をカウントする

email     suscriber_id 
---------------------------------------------- 
[email protected]   1 
[email protected]   1 
[email protected]   2 
[email protected]   3 
[email protected]   1 

私はこのクエリをしようとした場合1:

SELECT substring(email,LOCATE('@',email),LENGTH (email)) as domain, count(substring(email,LOCATE('@',email),LENGTH (email))) as counter from tracks group by domain"; 

domain     counter 
---------------------------------------------- 
yahoo.es    4 
gmail.com    1 

しかし、私はこの結果をしたい:あなたはCOUNT DISTINCTを探している

domain     counter 
---------------------------------------------- 
yahoo.es    2 
gmail.com    1 
+1

。 – Luke

+0

「blaCountblaからFROM(SELECT DISTINCT(email)from myTable)WHERE bla = bla'のようなものはどうですか? – Luke

答えて

2

。そして、あなたは簡単に2つのサブストリングにアクセスするためにSUBSTRING_INDEXを使用することができます:あなたは、これはより読みやすいかもしれません

select 
    substring_index(email, '@', -1) as domain, 
    count(distinct substring_index(email, '@', 1)) as counter 
from tracks 
group by substring_index(email, '@', -1); 

:あなたのテーブル内の重複を持っている理由私は思ったんだけど

select domain, count(distinct account) as counter 
from 
(
    select 
    substring_index(email, '@', -1) as domain, 
    substring_index(email, '@', 1) as account 
    from tracks 
) emails 
group by domain; 
+0

はうまく動作します。ありがとうございました! – h3llr4iser

関連する問題