Check for x consecutive days - given timestamps in database

后端 未结 4 1518
情深已故
情深已故 2020-11-27 16:32

Could anybody give me an idea or hint how you could check for X consecutive days in a database table (MySQL) where logins (user id, timestamp) are stored?

Stackoverf

4条回答
  •  春和景丽
    2020-11-27 17:16

    Wouldn't it be more simple to have an extra column consecutive_days in login_dates table with default value 1. This would indicate the length of consecutive dates ending on that day.

    You create an insert after trigger on login_dates where you check if there is an entry for the previous day.

    If there is none, then the field would have the default value 1 meaning that a new sequence is started on that date.

    If here is an entry for previous day then you change the days_logged_in value from the default 1 to be 1 greater then that of previous day.

    Ex:

    | date       | consecutive_days |
    |------------|------------------|
    | 2013-11-13 | 5                |
    | 2013-11-14 | 6                |
    | 2013-11-16 | 1                |
    | 2013-11-17 | 2                |
    | 2013-11-18 | 3                |
    

提交回复
热议问题