需要一个现有查询的唯一列

时间:2014-09-05 21:20:34

标签: mysql database

我在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可能有重复项,我不想在结果集中重复。我怎么能做到这一点?

1 个答案:

答案 0 :(得分: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