《MYSQL教程MYSQL實現連續簽到功能斷簽一天從頭開始(sql語句)》要點:
本文介紹了MYSQL教程MYSQL實現連續簽到功能斷簽一天從頭開始(sql語句),希望對您有用。如果有疑問,可以聯系我們。
MYSQL應用1,創建測試表
MYSQL應用
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表示簽到數據,1表示簽到日期字典數據'
) ENGINE=InnoDB DEFAULT CHARSET=utf8
MYSQL應用2,插入測試數據,簽到時間為5.21號到6.5號,可以寫活,但是要寫存儲過程,我比較懶,重點應該是取簽到數據的代碼,就是第三點,呵呵
MYSQL應用
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','吳小雙簽到數據','2017-05-21 00:00:00','0');
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數據','2017-05-22 00:00:00','0');
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數據','2017-05-23 00:00:00','0');
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數據','2017-05-24 00:00:00','0');
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數據','2017-05-25 00:00:00','0');
insert into `testsign` (`userid`, `username`, `signtime`, `type`) values('800675','吳小雙簽到數據','2017-05-26 00:00:00','0');
MYSQL應用3,查詢連續簽到數據
MYSQL應用
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
MYSQL應用未斷數據
MYSQL應用
MYSQL應用刪掉23號數據,從24號開始算,連續簽三天
MYSQL應用
MYSQL應用以上所述是小編給大家介紹的MYSQL實現連續簽到功能斷簽一天從頭開始,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的.在此也非常感謝大家對維易PHP網站的支持!
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/1045.html