计算每日物品

时间:2019-01-25 14:35:41

标签: mysql

我正在使用以下命令获取日常用品。 列日期是unix时间戳。

SELECT FROM_UNIXTIME(date) as datetime, COUNT(id) AS total 
FROM items 
WHERE cat_id = 3 
GROUP BY datetime 
HAVING datetime BETWEEN DATE_FORMAT(NOW() ,'%Y-%m-01') AND NOW()

结果是:

datetime |总计

2019-01-11 09:39:12 | 1

2019-01-11 09:44:35 | 1

2019-01-11 09:47:55 | 1

2019-01-15 14:33:00 | 1

2019-01-15 14:34:31 | 1

2019-01-17 14:39:26 | 1

实际上我正在寻找第二个结果:

日期时间|总计

2019-01-11 09:39:12 | 3

2019-01-15 14:33:00 | 2

2019-01-17 14:39:26 | 1

2 个答案:

答案 0 :(得分:1)

您应按DATE(而不是DATE和TIME)分组

SELECT  
  DATE(FROM_UNIXTIME(`date`)) AS `datetime`,
  COUNT(`id`) AS `total`
FROM `items`
WHERE `cat_id` = 3
GROUP BY DATE(datetime)
HAVING datetime BETWEEN DATE_FORMAT(NOW(), '%Y-%m-01') AND NOW()

答案 1 :(得分:0)

将您的GROUP BY更改为GROUP BY DATE(datetime)