2014-09-05 23 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

相关问题