小編給大家分享一下如何使用mysql計算本年用戶大未登錄時間段,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
目前創(chuàng)新互聯(lián)公司已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計、下花園網(wǎng)站維護(hù)等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。 計算本年用戶大未登錄時間段
space_user_task_statistics_log 如果用戶登錄過,則將信息記入該表
主要信息有用戶ID和登錄時間
space_user 記錄用戶基本信息. 主要包括用戶ID和注冊時間信息.
需求:
查所有用戶在2017年大的未登錄時間段
如果用戶在2017年才注冊,那么有效時間從注冊時間開始算。
其實(shí)這是一個MySQL計算連續(xù)范圍的問題。
SQL
<ol start="1" class="dp-sql" white-space:normal;">
select userid,startdate,enddate,diff from (
select t3.*,when userid=@gid then @rn:=@rn+1 when @gid:=userid then @rn:=1 end rank
from (
select userid,dt-interval rn min(dt) startdate,max(dt) enddate,
datediff(max(dt),min(dt)) diff
from (
select
cal.dt,cal.userid,when cal.userid=@gid then @rn:=@rn+1 when @gid:=cal.userid then @rn:=1 end rn
from
(
select dt,userid from (
select '2017-01-01'+interval id-1 from nums,(select @gid:=0,@rn:=0) vars where id<=dayofyear(now())
) var,
(
select u.userid,u.registertime from space_user u
where exists(select * from space_user_task_statistics_log l where l.createDate>='2017-01-01' and l.userid=u.userid)
) userlist
where dt>=date(userlist.registertime)
) cal
join
(
select log.userid,log.createDate from
space_user_task_statistics_log log
where log.loginflag=1 and log.createDate>='2017-01-01'
) t1 on(t1.userid=cal.userid and cal.dt=t1.createDate)
where createdate is null
order by userid,dt
) t2
group by userid,dt-interval rn order by userid,diff desc
) t4 where rank=1
order by diff desc;
生產(chǎn)查詢一次耗時10分鐘左右.
diff 是大未登錄時間段的天數(shù).
以上是“如何使用mysql計算本年用戶大未登錄時間段”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
新聞標(biāo)題:如何使用mysql計算本年用戶最大未登錄時間段-創(chuàng)新互聯(lián)
轉(zhuǎn)載來源:http://vcdvsql.cn/article42/cssoec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計、軟件開發(fā)、網(wǎng)站排名、自適應(yīng)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容