可能重复:
SQL Count records within a month using a unix timestamp
我有一张这样的表
+--------+------------+ | id | datetime | +--------+------------+ | 1 | 1313405609 | | 1 | 1313144410 | | 2 | 1313405550 | | 1 | 1313405549 | +--------+------------+
datetime是php unix时间,我想按日期分组(例如20120517)并计算每个组项目。因为1313405609,1313405550和1313405549的日期是20110815,我想要的结果是:
+--------+------------+------------+ | id | datetime | Count | +--------+------------+------------+ | 1 | 1313405609 | 2 | | 1 | 1313144410 | 1 | | 2 | 1313405550 | 1 | +--------+------------+------------+
我怎么能用sql做到?
答案 0 :(得分:1)
使用FROM_UNIXTIME
将unix时间戳转换为日期时间。然后使用DATE
仅获取日期部分:
select
id,
min(datetime) as datetime,
count(*) as Count
from
YourTable t
group by
id,
date(from_unixtime(datetime))