小編給大家分享一下MySQL如何實(shí)現(xiàn)連續(xù)簽到斷簽一天從頭開(kāi)始的功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
鐵東ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書(shū)未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書(shū)銷(xiāo)售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書(shū)合作)期待與您的合作!
1,創(chuàng)建測(cè)試表
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,插入測(cè)試數(shù)據(jù),簽到時(shí)間為5.21號(hào)到6.5號(hào),可以寫(xiě)活,但是要寫(xiě)存儲(chǔ)過(guò)程,我比較懶,重點(diǎn)應(yīng)該是取簽到數(shù)據(jù)的代碼,就是第三點(diǎn),呵呵
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ún)連續(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ù)
刪掉23號(hào)數(shù)據(jù),從24號(hào)開(kāi)始算,連續(xù)簽三天
以上是MYSQL如何實(shí)現(xiàn)連續(xù)簽到斷簽一天從頭開(kāi)始的功能的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!