简体   繁体   English

MySQL查询按日期对表中的条目数进行计数,并按日/周/月分组

[英]MySQL query to count number of entries in a table by date and grouped by day/week/month

I want to count the number of users in my MySQL auth_users table by the date they are added and group them into users per day, per week, and per month. 我想按添加日期对MySQL auth_users表中的用户数进行计数,并将它们每天,每周和每月分组为用户。

The only way I can think of to do this is to do a simple MySQL query like this: 我想到的唯一方法是执行一个简单的MySQL查询,如下所示:

SELECT date_joined FROM users

And then write some Python to iterate through the table checking for specific date periods. 然后编写一些Python来遍历表以检查特定日期周期。

I was wondering though if there are any MySQL queries or Python utilities to do this? 我想知道是否有任何MySQL查询或Python实用程序可以做到这一点?

EDIT 编辑

I have tried the answer pointed to but it isn't working for me. 我已经尝试了所指向的答案,但是它对我没有用。 I have tried the query: 我试过查询:

SELECT COUNT(id) FROM ebdb.auth_user GROUP BY date_joined.YEAR, date_joined.MONTH

date_joined is a DATETIME field in my database table ebdb.auth_user. date_joined是我的数据库表ebdb.auth_user中的DATETIME字段。 I get an MySQL error: 我收到一个MySQL错误:

Unknown column 'date_joined.YEAR' 未知列“ date_joined.YEAR”

Try the MySQL EXTRACT function. 尝试使用MySQL EXTRACT函数。

SELECT COUNT(id), EXTRACT(MONTH FROM date_joined) as month, EXTRACT(YEAR FROM date_joined) as year
FROM auth_user
GROUP BY month, year

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM