2014-09-05 48 views
0

我有一個日誌表在MySQL中,看起來像這樣:需要一個唯一的列到現有的查詢

table1 

id client_id country result 
------------------------------- 
1 323323  US  escalated 

我當前的查詢是:

select country, count(country) as country_total 
    FROM table1 
    WHERE result = 'escalated' 
    GROUP BY country 
    ORDER BY country_total 
    DESC LIMIT 5 

結果(這正是我想要的)是:

$VAR1 = [ 
     { 
     'country' => 'US', 
     'country_total' => '30' 
     }, 
     { 
     'country' => 'CN', 
     'country_total' => '19' 
     }, 
     { 
     'country' => 'DE', 
     'country_total' => '10' 
     } 
    ]; 

然而,在日誌表client_ids可以有重複,我不想在我的結果集中的重複。我怎樣才能做到這一點?

回答

2

使用distinct

SELECT country, count(distinct client_id) as country_total 
FROM table1 
WHERE result = 'escalated' 
GROUP BY country 
ORDER BY country_total 
DESC LIMIT 5 
+0

aaaaaaaaaaaaand它變得如此清晰,因爲我的大腦放屁已被清除。謝謝。 – Kizzim 2014-09-05 21:34:02

相關問題