可能重复:
How do I make a row generator in mysql
generate_series equivalent in mysql
我在创建mysql Query时遇到了麻烦。
我的PHP脚本在每次运行时执行此查询: INSERT INTO执行SET UserIp ='%s'(%s是用户IP)
执行表是:
ExecuteId UNSIGNED BIGINT AI PRIMARY
Date TIMESTAMP DEFAULT CURRENT_TIMESTAMP INDEX
UserIp CHAR(24) ... | Some Columns
我想每小时检索一次Executes。最明显的解决方案是:
SELECT COUNT(*) as ExecutesNum, DATE(Date) as D, HOUR(Date) as H GROUP BY D, H
它可以工作,但它不会在没有执行的情况下创建几小时的行。
我应该修改什么来获得结果:
1 | 2012-09-01 | 14
**0 | 2012-09-01 | 15**
11 | 2012-09-01 | 16
1 | 2012-09-01 | 17
答案 0 :(得分:2)
这是一个相当常见的问题,我通常通过创建一个包含所有小时的临时表来解决这个问题,如下所示:
DROP TABLE IF EXISTS hours;
CREATE TABLE hours (hour VARCHAR(13) PRIMARY KEY);
DROP PROCEDURE IF EXISTS fill_hours;
DELIMITER |
CREATE PROCEDURE fill_hours(start_time DATETIME, end_time DATETIME)
BEGIN
DECLARE crt_time DATETIME;
SET crt_time=DATE_SUB(start_time, INTERVAL DATE_FORMAT(start_time, '%i:%s') MINUTE_SECOND);
WHILE crt_time < end_time DO
INSERT INTO hours VALUES(DATE_FORMAT(crt_time, '%Y-%m-%d-%H'));
SET crt_time = DATE_ADD(crt_time, INTERVAL 1 HOUR);
END WHILE;
END |
CALL fill_hours( (SELECT MIN(Date) FROM Executes), (SELECT MAX(Date) FROM Executes) );
然后,您可以将此表格连接到原始表格以获得所需内容:
SELECT
h.hour,
COUNT(e.ExecuteId)
FROM hours h
LEFT JOIN Executes e ON DATE_FORMAT(e.Date, "%Y-%m-%d-%H") = h.hour
GROUP BY h.hour