欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL如何計(jì)算連續(xù)登錄天數(shù)

 更新時(shí)間:2022年05月09日 09:37:25   作者:BurningSilence  
這篇文章主要介紹了MySQL如何計(jì)算連續(xù)登錄天數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

建表、insert數(shù)據(jù)

create table tmp_login (
  user_id int(11) ,
  login_date datetime
);
insert into tmp_login values(2,'2020-05-29 11:12:12');
insert into tmp_login values(2,'2020-05-29 15:12:12');
insert into tmp_login values(2,'2020-05-30 11:12:12');
insert into tmp_login values(2,'2020-05-31 11:12:12');
insert into tmp_login values(2,'2020-06-01 11:12:12');
insert into tmp_login values(2,'2020-06-02 11:12:12');
insert into tmp_login values(2,'2020-06-03 11:12:12');
insert into tmp_login values(2,'2020-06-04 11:12:12');
insert into tmp_login values(2,'2020-06-05 11:12:12');
insert into tmp_login values(2,'2020-06-06 11:12:12');
insert into tmp_login values(2,'2020-06-07 11:12:12');
insert into tmp_login values(7,'2020-06-01 11:12:12');
insert into tmp_login values(7,'2020-06-02 11:12:12');
insert into tmp_login values(7,'2020-06-03 11:12:12');
insert into tmp_login values(7,'2020-06-05 11:12:12');
insert into tmp_login values(7,'2020-06-06 11:12:12');
insert into tmp_login values(7,'2020-06-07 11:12:12');
insert into tmp_login values(7,'2020-06-08 11:12:12');

方法一 row_number()

1.查詢(xún)所有用戶(hù)的每日登錄記錄

select distinct user_id, date(login_date) as days?
from tmp_login;

在這里插入圖片描述

2.row_number()計(jì)算登錄時(shí)間排序

select user_id, days, row_number() over(partition by user_id order by days) as rn
from (
	select distinct user_id, date(login_date) as days from tmp_login) t1;

在這里插入圖片描述

3.用登錄時(shí)間 - row_number(),如果得到的日期相同,則認(rèn)為是連續(xù)登錄日期

select *, date_sub(days, interval rn day) as  results
from(
	select user_id, days, row_number() over(partition by user_id order by days) as rn
	from (
		select distinct user_id, date(login_date) as days from tmp_login) t1
) t2;

在這里插入圖片描述

4. 按user_id、results分組就可得出連續(xù)登錄天數(shù)

select user_id, count(*) as num_days
from (
	select *, date_sub(days, interval rn day) as  results
	from(
		select user_id, days, row_number() over(partition by user_id order by days) as rn
		from (
			select distinct user_id, date(login_date) as days from tmp_login) t1
	) t2) t3
group by user_id , results;

在這里插入圖片描述

直接用日期減去row_number(),不用date_sub的話,遇到登錄日期跨月時(shí)會(huì)計(jì)算錯(cuò)誤,

方法二lead() 或 lag()

這種情況適合的場(chǎng)景是,需要查找連續(xù)登錄超過(guò)n天的用戶(hù),n為確定值

如果n為4,即計(jì)算連續(xù)登錄超過(guò)4天的用戶(hù)

-- lead計(jì)算連續(xù)登錄
select distinct user_id 
from(
	select user_id, days, datediff(lead(days, 3, '1970-01-01') over(partition by user_id order by days), days)as results
	from (
		select distinct user_id, date(login_date) as days from tmp_login) t1) t2
where results = 3;

連續(xù)登錄4天,則日期差應(yīng)該為3。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • mysql對(duì)于模糊查詢(xún)like的一些匯總

    mysql對(duì)于模糊查詢(xún)like的一些匯總

    這篇文章主要給大家介紹了關(guān)于mysql對(duì)于模糊查詢(xún)like的一些匯總,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Navicat連接linux虛擬機(jī)上的MySQL可能遇到的問(wèn)題及排查方法

    Navicat連接linux虛擬機(jī)上的MySQL可能遇到的問(wèn)題及排查方法

    這篇文章主要介紹了Navicat連接linux虛擬機(jī)上的MySQL可能遇到的問(wèn)題以及如何排查,本文給大家展示了問(wèn)題描述及解決方法,需要的朋友可以參考下
    2024-02-02
  • 詳解關(guān)于MySQL 8.0走過(guò)的坑

    詳解關(guān)于MySQL 8.0走過(guò)的坑

    這篇文章主要介紹了詳解關(guān)于MySQL 8.0走過(guò)的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • Linux下卸載MySQL數(shù)據(jù)庫(kù)

    Linux下卸載MySQL數(shù)據(jù)庫(kù)

    如何在Linux平臺(tái)卸載MySQL呢?這篇文章主要介紹了Linux下卸載MySQL數(shù)據(jù)庫(kù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • mysql心得分享:存儲(chǔ)過(guò)程

    mysql心得分享:存儲(chǔ)過(guò)程

    MySQL 5.0以后的版本開(kāi)始支持存儲(chǔ)過(guò)程,存儲(chǔ)過(guò)程具有一致性、高效性、安全性和體系結(jié)構(gòu)等特點(diǎn),本文主要來(lái)分享下本人關(guān)于存儲(chǔ)過(guò)程的一些心得體會(huì)。
    2014-07-07
  • Mysql入門(mén)基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇

    Mysql入門(mén)基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇

    Mysql入門(mén)基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇,剛接觸php與mysql的朋友可以參考下。多寫(xiě)多測(cè)試。
    2010-04-04
  • idea中使用mysql的保姆級(jí)教程(超詳細(xì))

    idea中使用mysql的保姆級(jí)教程(超詳細(xì))

    我們開(kāi)發(fā)時(shí)經(jīng)常需要用到一些客戶(hù)端去訪問(wèn)數(shù)據(jù)庫(kù)查詢(xún)、更新數(shù)據(jù)等操作,下面這篇文章主要給大家介紹了關(guān)于idea中使用mysql的保姆級(jí)教程,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • Linux下編譯安裝Mysql 5.5的簡(jiǎn)單步驟

    Linux下編譯安裝Mysql 5.5的簡(jiǎn)單步驟

    Linux下面因?yàn)閺腗ySQL 5.5開(kāi)始使用cmake來(lái)做config了,所以編譯安裝的會(huì)和5.1版本有些區(qū)別。不過(guò)總體來(lái)說(shuō)還是差別不大
    2015-08-08
  • MySQL基本運(yùn)維命令詳解

    MySQL基本運(yùn)維命令詳解

    這篇文章主要介紹了MySQL基本運(yùn)維命令,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • MySQL讀取JSON轉(zhuǎn)換的方式

    MySQL讀取JSON轉(zhuǎn)換的方式

    這篇文章主要介紹了MySQL讀取JSON轉(zhuǎn)換的方式,本文給大家分享兩種方式給大家講解處理方式,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-03-03

最新評(píng)論