明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

MYSQL如何完成連續(xù)簽到斷簽一天從頭開始的技巧詳細說明(圖)

[摘要]這篇文章主要介紹了MYSQL實現(xiàn)連續(xù)簽到功能斷簽一天從頭開始,非常不錯,具有參考借鑒價值,需要的朋友可以參考下1,創(chuàng)建測試表CREATE TABLE `testsign` ( `userid` int(5) DEFAULT NULL, `username` varchar(20) DEFAULT ...
這篇文章主要介紹了MYSQL實現(xiàn)連續(xù)簽到功能斷簽一天從頭開始,非常不錯,具有參考借鑒價值,需要的朋友可以參考下

1,創(chuàng)建測試表

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表示簽到數(shù)據(jù),1表示簽到日期字典數(shù)據(jù)' 
) ENGINE=InnoDB DEFAULT CHARSET=utf8

2,插入測試數(shù)據(jù),簽到時間為5.21號到6.5號,可以寫活,但是要寫存儲過程,我比較懶,重點應該是取簽到數(shù)據(jù)的代碼,就是第三點,呵呵

insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-21 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-22 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-23 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-24 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-25 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-26 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-27 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-28 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-29 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-30 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-05-31 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-06-01 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-06-02 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-06-03 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-06-04 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('72164','字典','2017-06-05 00:00:00','1'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-21 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-22 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-23 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-24 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-25 00:00:00','0'); 
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數(shù)據(jù)','2017-05-26 00:00:00','0');

3,查詢連續(xù)簽到數(shù)據(jù)

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

未斷數(shù)據(jù)

MYSQL如何實現(xiàn)連續(xù)簽到斷簽一天從頭開始的功能詳解(圖)

刪掉23號數(shù)據(jù),從24號開始算,連續(xù)簽三天

MYSQL如何實現(xiàn)連續(xù)簽到斷簽一天從頭開始的功能詳解(圖)

以上就是MYSQL如何實現(xiàn)連續(xù)簽到斷簽一天從頭開始的功能詳解(圖)的詳細內(nèi)容,更多請關注php中文網(wǎng)其它相關文章!


學習教程快速掌握從入門到精通的SQL知識。