所以我有一个数据库表,其中包含id,action,timestamp。我希望通过相应的点击/视图获得独特的日子。
这样的事情:
Date ------ Hits
2018-01-10 5
2018-01-11 542
2018-01-12 74
我的方法是:
SELECT DISTINCT FROM_UNIXTIME (timecreated, '%d.%m.%y') AS 'date'
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
这列出了我所有独特的日子,但如果我将count(action) AS 'hits'
添加到SELECT语句中,它只会向我显示所有匹配的总和的第一天。
如何解决此查询?我很感激任何提示
答案 0 :(得分:0)
您需要使用GROUP BY
对结果进行分组SELECT FROM_UNIXTIME (timecreated, '%d.%m.%y') AS `date`, COUNT(action) AS hits
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
GROUP BY FROM_UNIXTIME (timecreated, '%d.%m.%y')
ORDER BY `date`
答案 1 :(得分:0)
对于您要求的输出,您应该这样做:
SELECT FROM_UNIXTIME(timecreated, '%Y-%m-%d') AS `date`, COUNT(*) AS hits
FROM `mdl_logstore_standard_log`
WHERE action = 'viewed'
GROUP BY FROM_UNIXTIME (timecreated, '%Y-%m-%d')
ORDER BY MIN(timecreated);
order by
使用MIN(timecreated)
,因此即使您更改格式也会有效。