1 回答
TA贡献1852条经验 获得超7个赞
您可以使用“数字”表在一小时 (0-3) 内生成 15 分钟的时间段,然后LEFT JOIN根据您的计数,按 15 分钟的时间段分组,使用0COALESCE替换值:NULL
SELECT periods.period * 900 + 1592352000 AS timestamp,
FROM_UNIXTIME(periods.period * 900 + 1592352000) AS time,
COALESCE(counts.total, 0) AS total
FROM (
SELECT 0 period
UNION ALL SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
) periods
LEFT JOIN (
SELECT (timestamp - 1592352000) DIV 900 AS period,
COUNT(*) AS total
FROM requests
WHERE timestamp >= 1592352000 AND timestamp < 1592352000 + 3600
GROUP BY period
) counts ON counts.period = periods.period
输出(对于您问题中的数据加上其他几个值):
timestamp time total
1592352000 2020-06-17 00:00:00 1
1592352900 2020-06-17 00:15:00 1
1592353800 2020-06-17 00:30:00 5
1592354700 2020-06-17 00:45:00 0
- 1 回答
- 0 关注
- 129 浏览
添加回答
举报