MySQL - SELECT:根据当前日期计算结果,间隔步长 - 默认计数值

时间:2016-11-07 15:39:11

标签: mysql

我有一个消息日志表,包含不同类型的日志。 由于我对MySQL不那么热衷,我想从更有经验的MySQL配置文件中得到一些建议:)

我正在寻找一个SQLQuery来计算当天的INTERVAL为1小时的AMOUNT OF MESSAGE日志。

如果该间隔没有结果,则默认值应设置为“0”。

Created                Message       
-----------------------------------------------
2016-11-07 01:42:06     "Message content here"
2016-11-07 04:10:04     "Message content here"
2016-11-07 12:10:04     "Message content here"
2016-11-07 13:45:24     "Message content here"
2016-11-07 16:22:54     "Message content here"
2016-11-07 17:11:54     "Message content here"
2016-11-07 19:23:54     "Message content here"
2016-11-07 20:15:54     "Message content here"
2016-11-07 20:51:54     "Message content here"
2016-11-07 20:51:57     "Message content here"
2016-11-07 20:51:58     "Message content here"
2016-11-07 20:51:59     "Message content here"
2016-11-07 20:52:05     "Message content here"
2016-11-07 21:02:05     "Message content here"

预期的回报应该是这样的,所以我可以将结果集可视化为图形数据。

2016-11-07 01:00:00 | 1
2016-11-07 02:00:00 | 0
2016-11-07 03:00:00 | 0
2016-11-07 04:00:00 | 1
2016-11-07 05:00:00 | 0
2016-11-07 06:00:00 | 0
2016-11-07 07:00:00 | 0
2016-11-07 08:00:00 | 0
2016-11-07 09:00:00 | 0
2016-11-07 10:00:00 | 0
2016-11-07 11:00:00 | 0
2016-11-07 12:00:00 | 1
2016-11-07 13:00:00 | 1
2016-11-07 14:00:00 | 0
2016-11-07 15:00:00 | 0
2016-11-07 16:00:00 | 1
2016-11-07 17:00:00 | 1
2016-11-07 18:00:00 | 0
2016-11-07 19:00:00 | 1
2016-11-07 20:00:00 | 6
2016-11-07 21:00:00 | 1
2016-11-07 22:00:00 | 0
2016-11-07 23:00:00 | 0
2016-11-07 00:00:00 | 0

谢谢!

1 个答案:

答案 0 :(得分:1)

仅列出出现一些流量的小时数

SELECT
 date_format(Created, '%Y-%m-%d %H:00:00'),
 count(Message)
FROM message_log 
GROUP BY date_format(Created, '%Y-%m-%d %H:00:00');

还列出"空"零时你可以使用类似的东西:

SET @starttime = '2015-10-10 01:00:00';
SET @endtime   = '2016-01-01 12:00:00'; 

SET @starttime_h = date_format( @starttime , '%Y-%m-%d %H:00:00');

SET @hours_count = TIMESTAMPDIFF(HOUR, @starttime, @endtime) + 1;

SELECT hours.hour, COUNT(message_log.Message) FROM
( SELECT @starttime_h + INTERVAL ( (a.n) + (b.n<<4) + (c.n<<8) + (d.n<<12)) HOUR as hour
FROM  (select 0 as n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union select 10 union select 11 union select 12 union select 13 union select 14 union select 15) a
  cross join (select 0 as n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union  select 10 union select 11 union select 12 union select 13 union select 14 union select 15) b
  cross join (select 0 as n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union  select 10 union select 11 union select 12 union select 13 union select 14 union select 15) c
  cross join (select 0 as n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9 union  select 10 union select 11 union select 12 union select 13 union select 14 union select 15) d
WHERE (a.n) + (b.n<<4) + (c.n<<8) + (d.n<<12) <= @hours_count ) hours LEFT JOIN 
message_log  ON (hours.hour = date_format(message_log.Created, '%Y-%m-%d %H:00:00')) 
WHERE hours.hour BETWEEN @starttime AND @endtime 
GROUP BY hours.hour;