【问题标题】:MySQL query to return a daily 30-day countMySQL query to return a daily 30-day count
【发布时间】:2022-11-09 11:00:12
【问题描述】:

Let's say that one has a pair of tables:

users

user_id email
1 email1@test.com
2 email2@test.com
... ...

logins

login_id user_id date_login
1 1 2038-01-19 03:14:07
2 1 2038-01-19 03:14:08
3 2 2038-01-19 03:14:08
... ... ...

and one wanted to get a list of totals of "active users" for each day of the last year; where "active user" is defined as anyone who had logged in within 30 days of each date in question.

e.g. result: | date_count | activeuser_count | | -------- | -------------- | | 2038-01-19 | 2345 | | 2038-01-20 | 3456 | | 2038-01-21 | 4321 | | ... | ... |

How would one do this most efficiently / with the fewest number of queries?

    标签: mysql


    【解决方案1】:

    select * from users inner join logins on users.user_id = logins.user_id where date_diff(now(), date_login) <= 30

    • Sorry: was looking for the total number of active users for each day, not a list of the active users for each day
    猜你喜欢
    • 2022-11-20
    • 1970-01-01
    • 2022-08-12
    • 2022-12-26
    • 2022-12-02
    • 2022-12-01
    • 2015-05-19
    • 2012-04-13
    • 2022-12-02
    相关资源
    最近更新 更多