doufenpaiyu63706 2015-12-23 03:43
浏览 565
已采纳

您如何查询MySQL数据库以确定用户是否已连续30天或100登录?

How could you query a MySQL database to determine if a user has logged in for 30 consecutive days or 100? Same will apply for other user actions too.

Assuming a DB table like below where a record is created each day for each user active on that day.

How could I query SELECT * WHERE user_id = 1 AND action = login and find if there was 30 days or 100 days of logins where the days are in consecutive row with no day breaks in it?

User Stats Table

id  
user_id  
dateTime  
action (login, made post, up voted, down votes, made comment, etc...)
  • 写回答

1条回答 默认 最新

  • douhong1703 2015-12-23 03:49
    关注

    In MySQL, probably the easiest way to do this is to use variables. Something like this:

    select us.*
    from (select us.* ,
                 (@rn := if(date(@d) = date(datetime), @rn,
                            if(date(@d) = date(datetime) - interval 1 day,
                               if(@d := date(@datetime), @rn + 1, @rn + 1)
                               if(@d := date(@datetime), 1, 1)
                              )
                           )
                ) as rn
          from userstats us cross join
               (select @d := '', @rn := 0) params
          where user_id = 1
          order by datetime
         ) us
    where rn >= 100;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退