Mysql选择按日期字段的一部分进行分组

时间:2016-11-01 17:26:52

标签: php mysql sql database

我有一个应用程序日志表。我目前使用以下查询计算每分钟的操作数

SELECT COUNT(id) 
FROM application_log 
WHERE activity_date LIKE '%2016-11-01 18:12%' AND activity_type = 'Full Invoice' AND status = 'Success'

目前,我必须更改每一分钟并重新运行查询。

我有什么方法可以在一小时内得到每分钟的计数?或者进一步获取一天中每小时的行动次数?

2 个答案:

答案 0 :(得分:1)

使用DATE_FORMAT将时间缩短到几分钟,并按此分组。

SELECT DATE_FORMAT(activity_date, '%H:%i') AS time, COUNT(*)
FROM application_log
WHERE activity_date BETWEEN @start_time AND @end_time
AND activity_type = 'Full Invoice' AND status = 'Success'
GROUP BY time
ORDER BY time

如果时间范围超过一天,请将日期添加到格式字符串。

答案 1 :(得分:0)

希望这有帮助

SELECT  concat(date(activity_date),' ',hour(activity_date),':', minute(activity_date)) as activity_date , COUNT(id) 
FROM application_log 
WHERE activity_type = 'Full Invoice' AND status = 'Success' 
group by date(activity_date),hour(activity_date), minute(activity_date);