这一定是一件普通的事情,但是我被困住了.
我有一些可以像这样简化的数据:
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,但是不能让它按预期工作 – 任何想法?
解决方法:
SELECT
DATE(FROM_UNIXTIME(unixtime)) as date,
HOUR(FROM_UNIXTIME(unixtime)) AS hour,
count(c.user) AS count
FROM core c
GROUP BY date,hour
如果您希望将小时作为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_前缀可避免保留字出现问题
网友评论