我的php时间戳(例如1307362819)存储在我的数据库的一列中。我想在数天,周和月内对计数(*)进行分组。
例如,我想知道每天,每周和每月有多少条目。
如何实现这一目标?
答案 0 :(得分:4)
您可以从时间戳中减去日,周和月值,并对减去的值进行分组。
按日值分组:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m%d')
按周值分组:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m%u')
按月值分组:
select count(*) from table group by from_unixtime(timeStampColumn, '%Y%m')
有关详细信息,请查看此页面:http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html
答案 1 :(得分:0)
为什么不使用这些:<>
SELECT COUNT(*) FROM yourTable WHERE yourTimestampField > someTimestampAWeekPast;
SELECT COUNT(*) FROM yourTable WHERE yourTimestampField > someTimestampADayPast;
使用mysql日期/时间函数http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html计算过去的所需日期。 如果将它们存储为int或string或其他任何内容,请使用UNIX_TIMESTAMP。
答案 2 :(得分:0)
这里有一个很好的例子
编辑:试试这个
select
id_user,
year(time) as AYear, week(time) as AWeek, day(time) as Aday ,
count(week(time)) as TotalPerWeek , count(day(time)) as TotalPerDay ,
count(year(time)) as TotalPerYear
from yourtable
where id_user = 16 //// the user u want to check
group by id_user, AYear, AWeek , Aday
order by AYear, AWeek , Aday
答案 3 :(得分:-3)
SELECT MONTH( FROM_UNIXTIME( `timeStamp` ) ) , COUNT( `id` )
FROM `discusComments`
GROUP BY MONTH( FROM_UNIXTIME( `timeStamp` ) )