6

这一定是一件很常见的事情,但我被困住了。

我有一些可以像这样简化的数据:

id    user    unixtime
-----------------------
1     dave    1335312057
2     dave    1335312058
3     steve   1335312128

等等

到目前为止,我只需要按天汇总,所以我一直在使用:

SELECT
 UNIX_TIMESTAMP(DATE(FROM_UNIXTIME(unixtime))) AS time,
 count(c.user) AS count
FROM core c
GROUP BY DATE(FROM_UNIXTIME(unixtime))

我尝试过将 CONCAT 与 DATE 和 HOUR 一起使用,但不能完全按预期工作——有什么想法吗?

4

2 回答 2

5
SELECT
 DATE(FROM_UNIXTIME(unixtime)) as date,
 HOUR(FROM_UNIXTIME(unixtime)) AS hour,
 count(c.user) AS count
FROM core c
GROUP BY 1,2 

如果要将小时作为 unix 时间戳,请包装此查询以获取它:

SELECT UNIX_TIMESTAMP(DATE_ADD(the_date, INTERVAL the_hour HOUR)), the_count
from (select
    DATE(FROM_UNIXTIME(unixtime)) as the_date,
    HOUR(FROM_UNIXTIME(unixtime)) as the_hour,
    count(c.user) AS the_count
    FROM core c
    GROUP BY 1,2 
) x

注意:the_在列名上使用前缀以避免保留字的问题

于 2012-10-27T19:33:25.380 回答
1

尝试使用

SELECT
     UNIX_TIMESTAMP(DATE_ADD(DATE(FROM_UNIXTIME(unixtime)), INTERVAL HOUR(FROM_UNIXTIME(unixtime)) HOUR)) AS TIME,
     COUNT(c.user) AS COUNT
    FROM core c
    GROUP BY DATE(FROM_UNIXTIME(unixtime)), HOUR(FROM_UNIXTIME(unixtime));

希望这可以帮助...

于 2012-10-27T19:43:36.043 回答