0

我需要COUNT()(或者可能是另一个 MySQL 函数)我的查询行,但我需要不同的计数,如下所示:

我得到了什么:

   id  | date       | costumer_id
  -----+------- ----+-------------
   1   | 2012-10-05 | 103
   2   | 2012-10-05 | 103
   3   | 2012-10-05 | 103
   4   | 2012-10-05 | 59
   5   | 2012-10-05 | 59
   6   | 2012-10-05 | 90

我需要的:

   id  | date       | costumer_id | count
  -----+------------+-------------+------
   1   | 2012-10-05 | 103         | 3
   5   | 2012-10-05 | 59          | 2
   6   | 2012-10-05 | 90          | 1
4

4 回答 4

1

您需要一个简单的按查询分组。我怀疑您将 id 留在 group by 中。尝试这样的事情:

select max(id), date, customer_id, count(*)
from t
group by date, customer_id
order by 1
于 2012-10-05T17:52:30.917 回答
0
SELECT id, `date`, customer_id, COUNT(1) AS `count`
FROM `table`
GROUP BY `customer_id`
于 2012-10-05T17:52:41.003 回答
0

你必须使用GROUP BY

以下查询将为您提供所需的结果:

选择 id、日期、costumer_id 、count(costumer_id) as c from table1 group by costumer_id order by c desc

于 2012-10-05T17:58:59.647 回答
0
select 
    min(id) as id, 
    date, 
    customer_id, 
    count(*) as count
from tatble
group by 
    date, 
    customer_id
order by 1;
于 2012-10-05T18:03:19.290 回答