我有一个带有字段
的模式的mysql数据库用户名,时间戳
其中,时间戳是上次活动的日期。
我想在mysql中构建一个查询,它将为我提供特定用户按月分解的记录数。
感谢。
答案 0 :(得分:0)
您的问题是以每月的用户数量发布的,但我认为您的意思是指每个用户的每月记录数。您想要的查询是:
Select CAST(CONCAT(YEAR(`timestamp`),'-',MONTH(`timestamp)) AS CHAR) as `month`, COUNT(*) as `hits` FROM db.table WHERE `username`='<the_username>' GROUP BY `month`;
答案 1 :(得分:0)
Select count(*) as counta, DATE_FORMAT(timestamp, "%Y-%m") as "_month"
from users
where username='YOUR_USER_NAME'
group by _month
order by _month
对于unix时间戳,请使用FROM_UNIXTIME
SELECT count(*) as counta, DATE_FORMAT(FROM_UNIXTIME(timestamp), "%Y-%m") as "_month"
from users
where username='YOUR_USER_NAME'
group by _month
order by _month