我有这个循环:
BEGIN
DECLARE time INT DEFAULT 1479154801;
WHILE time <= 1481756399 DO
SELECT time;
SELECT COUNT(*) FROM phpbb_posts WHERE post_time BETWEEN time AND (time+86400);
SET time = time + 86400;
END WHILE;
END
我想要的是哪种工作,但结果没有分组,它们看起来像这样:
+------------+
| time |
+------------+
| 1479154801 |
+------------+
+----------+
| COUNT(*) |
+----------+
| 746 |
+----------+
+------------+
| time |
+------------+
| 1479241201 |
+------------+
+----------+
| COUNT(*) |
+----------+
| 652 |
+----------+
...
当我希望它看起来像这样:
+------------+----------+
| time | COUNT(*) |
+------------+----------+
| 1479154801 | 746 |
| 1479241201 | 652 |
...
我该怎么办?对于SQL来说,我是个新手,所以非常感谢你的帮助。
答案 0 :(得分:1)
听起来你白天想要的信息。这样做你想要的吗?
SELECT date(from_unixtime(post_time)), COUNT(*)
FROM phpbb_posts
GROUP BY date(from_unixtime(post_time));
我发现日期比Unix时间戳更直观。如果您不同意,可以使用unix_timestamp()
将值重新转换回来。
如果您更喜欢日期,则可以减去一小时:
SELECT date(date_sub(from_unixtime(post_time), interval 1 hour)) as dte, COUNT(*)
FROM phpbb_posts
GROUP BY date(date_sub(from_unixtime(post_time), interval 1 hour));
或者,正如评论建议的那样,从post_time
中减去/添加3600。