下面一起來了解下MySQL連續(xù)數(shù)據(jù)查詢的簡單方法,相信大家看完肯定會受益匪淺,文字在精不在多,希望Mysql連續(xù)數(shù)據(jù)查詢的簡單方法這篇短內(nèi)容是你想要的。
南鄭ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
順序行號 - 減首差值 = 連續(xù)差塊
順序行號 如同 oracle 中的 rownum 但mysql目前還沒有這個功能,所以只能通過局部變量來實現(xiàn),
減首差值 就是每條記錄與最開始記錄的差(需要保證這個差值與順序行號遞增值相同,當然如果本來就是自增值則不需要單獨計算)
只要 順序行號與減首差值保持相同遞增值則 連續(xù)差塊 值相同,就可以統(tǒng)計出連續(xù)長度
示例表:(以簡單的簽到表為例)
create table user_sign( id int unsigned primary key auto_increment, user_id int unsigned not null comment '用戶ID', date date not null comment '簽到日期', created_time int unsigned not null comment '創(chuàng)建時間', updated_time int unsigned not null comment '修改時間' )engine=innodb default charset=utf8 comment '用戶簽到';
隨機生成數(shù)據(jù)(創(chuàng)建函數(shù)隨機生成簽到數(shù)據(jù))
create function insert_sign_data(num int) returns int begin declare _num int default 0; declare _date date; declare _tmpdate date; declare _user_id int; declare line int default 0; declare _get_last cursor for select date from user_sign where user_id=_user_id order by date desc limit 1; declare continue handler for SQLSTATE '02000' set line = 1; while _num < num do set _user_id = CEIL( RAND( ) * 500 ); open _get_last; fetch _get_last into _tmpdate; IF line THEN set _date = FROM_UNIXTIME( unix_timestamp( ) - 86400 * round( RAND( ) * 200 ), '%Y-%m-%d' ); set line = 0; ELSE set _date = FROM_UNIXTIME( unix_timestamp( _tmpdate ) + 86400 * round( RAND( ) * 2 + 1), '%Y-%m-%d' ); END IF; INSERT INTO user_sign ( user_id, date, created_time, updated_time ) VALUES (_user_id, _date, unix_timestamp( ), unix_timestamp( )); set _num = _num + 1; close _get_last; end while; return _num; end
生成數(shù)據(jù)(由于生成時有判斷最近打卡日期生成有會點慢)
select insert_sign_data(20000);
提取出連續(xù)打卡超過6天的用戶
SELECT user_id, val - ( @rownum := @rownum + 1 ) AS type, group_concat( date ) AS date_join, count( 1 ) num FROM ( SELECT us1.date, us1.user_id, ( unix_timestamp( us1.date ) - min_timestamp ) / 86400 + 1 AS val FROM user_sign AS us1 LEFT JOIN ( SELECT UNIX_TIMESTAMP( min( date ) ) AS min_timestamp, user_id, min( date ) AS min_date FROM user_sign GROUP BY user_id ) AS us2 ON us1.user_id = us2.user_id ORDER BY us1.user_id ASC, us1.date ASC ) AS t1, ( SELECT @rownum := 0 ) AS t2 GROUP BY user_id, type HAVING num > 6
這里查詢的是全表里連續(xù)超過3次打卡的,并把日期展示出來。
查詢的思路是:
提取出全表用戶每次打卡記錄與第一次打卡記錄的差值但按用戶與日期正排序
增加一個局部變量rownum與上面查詢數(shù)據(jù)進行連查
在結果字段集里使用日期差值減去自增順序行號值得到連續(xù)差塊
通過分組用戶與連續(xù)差塊獲取連續(xù)簽到次數(shù)
通過having來提取超過6次簽到的用戶

看完Mysql連續(xù)數(shù)據(jù)查詢的簡單方法這篇文章后,很多讀者朋友肯定會想要了解更多的相關內(nèi)容,如需獲取更多的行業(yè)信息,可以關注我們的行業(yè)資訊欄目。
文章標題:Mysql連續(xù)數(shù)據(jù)查詢的簡單方法
文章起源:http://chinadenli.net/article38/ppdesp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)、響應式網(wǎng)站、網(wǎng)站設計、建站公司、手機網(wǎng)站建設、網(wǎng)頁設計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)