MYSQL implements the continuous sign-in function and starts from the beginning after one day of sign-in (sql statement)

MYSQL implements the continuous sign-in function and starts from the beginning after one day of sign-in (sql statement)

1. Create a test table

CREATE TABLE `testsign` ( 
 `userid` int(5) DEFAULT NULL, 
 `username` varchar(20) DEFAULT NULL, 
 `signtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
 `type` int(1) DEFAULT '0' COMMENT '0 represents sign-in data, 1 represents sign-in date dictionary data' 
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2. Insert test data. The sign-in time is from May 21 to June 5. It can be written live, but it is necessary to write a stored procedure. I am lazy. The focus should be on the code for getting the sign-in data, which is the third point. Haha

insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-21 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-22 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-23 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-24 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-25 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-26 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-27 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-28 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-29 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-30 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-05-31 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-06-01 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-06-02 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-06-03 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-06-04 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','Dictionary','2017-06-05 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-21 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-22 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-23 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-24 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-25 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','Wu Xiaoshuang sign-in data','2017-05-26 00:00:00','0');

3. Query continuous sign-in data

SELECT * FROM testsign WHERE TYPE=0 AND 
 DATE_FORMAT(signtime,'%Y%m%d')>( 
 SELECT IFNULL(MAX(DATE_FORMAT(signtime,'%Y%m%d')),"20170520") FROM testsign WHERE TYPE=1 
 AND DATE_FORMAT(signtime,'%Y%m%d')<=DATE_ADD(NOW(), INTERVAL -1 DAY) 
 AND DATE_FORMAT(signtime,'%Y%m%d') NOT IN ( 
    SELECT DATE_FORMAT(signtime,'%Y%m%d') FROM testsign WHERE TYPE=0 AND userid=800675 
    ) 
 ) 
 AND DATE_FORMAT(signtime,'%Y%m%d')<='20170605' 
 AND userid=800675

Uninterrupted data

Delete the data on the 23rd, and start counting from the 24th, and sign for three consecutive days

The above is the MYSQL implementation of the continuous sign-in function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

You may also be interested in:
  • SQLSERVER records the login time of the logged-in user (self-written script)
  • How to query the maximum number of consecutive login days between two dates in MySQL
  • How to query the maximum number of consecutive login days between two dates in MySQL
  • SQL query statement to find the number of consecutive login days of a user

<<:  A brief analysis of how to upgrade PHP 5.4 to 5.6 in CentOS 7

>>:  Detailed explanation of using scp command to copy files remotely in Linux

Recommend

Parsing Linux source code epoll

Table of contents 1. Introduction 2. Simple epoll...

Analysis of the reasons why Vue3 uses Proxy to implement data monitoring

Vue data two-way binding principle, but this meth...

Semanticization of HTML tags (including H5)

introduce HTML provides the contextual structure ...

JavaScript super detailed implementation of web page carousel

Table of contents Creating HTML Pages Implement t...

Summary of relevant knowledge points of ajax in jQuery

Preface Students who learn JavaScript know that A...

A brief discussion on macrotasks and microtasks in js

Table of contents 1. About JavaScript 2. JavaScri...

Five ways to implement inheritance in js

Borrowing Constructors The basic idea of ​​this t...

Vue uses echart to customize labels and colors

This article example shares the specific code of ...

How to build a standardized vmware image for kubernetes under rancher

When learning kubernetes, we need to practice in ...

Comprehensive summary of Vue3.0's various listening methods

Table of contents Listener 1.watchEffect 2.watch ...

Build a server virtual machine in VMware Workstation Pro (graphic tutorial)

The VMware Workstation Pro version I use is: 1. F...

A brief discussion on the mysql execution process and sequence

Table of contents 1:mysql execution process 1.1: ...

Three ways to create a gray effect on website images

I’ve always preferred grayscale images because I t...