2011-04-20 25 views
107

Dün cp'deki farklı ziyaretlerin sayısını toplamaya çalışıyorum, sonra sayın. Bu aynı site kimliği ile birden fazla sonuç çekiyor Nedense MySQL COUNT DISTINCT

SELECT 
    DISTINCT `user_id` as user, 
    `site_id` as site, 
    `ts` as time 
FROM 
    `cp_visits` 
WHERE 
    ts >= DATE_SUB(NOW(), INTERVAL 1 DAY) 

.... nasıl ben sadece çekin ve farklı site_id cp oturumların sayabilirim?

cevap

229
Select 
    Count(Distinct user_id) As countUsers 
    , Count(site_id) As countVisits 
    , site_id As site 
From cp_visits 
Where ts >= DATE_SUB(NOW(), INTERVAL 1 DAY) 
Group By site_id 
7

Bir grubu yan tümce ile kullanmanız gerekir.

SELECT site_id, MAX(ts) as TIME, count(*) group by site_id 
17

Genel

SELECT 
     COUNT(DISTINCT `site_id`) as distinct_sites 
    FROM `cp_visits` 
WHERE ts >= DATE_SUB(NOW(), INTERVAL 1 DAY) 

Veya sitede

SELECT 
     `site_id` as site, 
     COUNT(DISTINCT `user_id`) as distinct_users_per_site 
    FROM `cp_visits` 
    WHERE ts >= DATE_SUB(NOW(), INTERVAL 1 DAY) 
GROUP BY `site_id` 

mantıklı değil sonuçta time sütunu olması başına

- Eğer satırlar toplayarak çünkü birini göstermesi min veyaolmadığı sürece, belirli time alakasız 210 peşindesin.