1 回答

TA貢獻(xiàn)1852條經(jīng)驗 獲得超7個贊
您可以使用“數(shù)字”表在一小時 (0-3) 內(nèi)生成 15 分鐘的時間段,然后LEFT JOIN根據(jù)您的計數(shù),按 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
輸出(對于您問題中的數(shù)據(jù)加上其他幾個值):
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 關(guān)注
- 171 瀏覽
添加回答
舉報