我有一个带有字段架构的 mysql 数据库
用户名,时间戳
其中时间戳是上次活动的日期。
我想在 mysql 中构建一个查询,它将为我提供按月分解的特定用户的记录数。
谢谢。
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
您的问题是以每月用户数的形式发布的,但我认为您的意思是给定用户每月的记录数。您想要的查询是:
Select CAST(CONCAT(YEAR(`timestamp`),'-',MONTH(`timestamp)) AS CHAR) as `month`, COUNT(*) as `hits` FROM db.table WHERE `username`='<the_username>' GROUP BY `month`;