sql连续登录

1、sql建表语句

sql 复制代码
DROP TABLE IF EXISTS `app_login_record`;
CREATE TABLE `app_login_record`  (
  `user_id` int(0) NULL DEFAULT NULL,
  `enter_time` datetime(0) NULL DEFAULT NULL,
  `leave_time` datetime(0) NULL DEFAULT NULL
);


INSERT INTO `app_login_record` VALUES (789012, '2023-05-03 17:52:00', '2023-05-03 17:55:00');
INSERT INTO `app_login_record` VALUES (789013, '2023-05-04 12:02:00', '2023-05-04 12:03:00');
INSERT INTO `app_login_record` VALUES (789012, '2023-05-02 13:36:00', '2023-05-02 13:45:00');
INSERT INTO `app_login_record` VALUES (789001, '2023-05-08 14:25:00', '2023-05-08 14:32:00');
INSERT INTO `app_login_record` VALUES (789003, '2023-05-10 10:46:00', '2023-05-10 10:47:00');
INSERT INTO `app_login_record` VALUES (789001, '2023-05-07 08:12:00', '2023-05-07 08:14:00');
INSERT INTO `app_login_record` VALUES (789012, '2023-05-04 16:32:00', '2023-05-04 16:35:00');
INSERT INTO `app_login_record` VALUES (789012, '2023-05-08 11:58:00', '2023-05-08 12:01:00');
INSERT INTO `app_login_record` VALUES (789001, '2023-05-09 19:35:00', '2023-05-09 19:38:00');
INSERT INTO `app_login_record` VALUES (789003, '2023-05-11 20:35:00', '2023-05-11 20:38:00');
INSERT INTO `app_login_record` VALUES (789009, '2023-05-01 22:58:00', '2023-05-01 23:03:00');

2、请找出连续3天登录小程序且浏览时长大于2分钟的用户

第1种方法(没考虑,一天登录2次的情况))、解:

sql 复制代码
select distinct user_id 
from(
SELECT * ,
               CASE
               WHEN DATE_SUB(str_to_date(enter_time,'%Y-%m-%d'),INTERVAL 1 DAY) = str_to_date(@old,'%Y-%m-%d')  and @u_id=user_id and @old:=enter_time 
					THEN @size:=@size+1
					
               WHEN @old:=enter_time 
					THEN @size:=1 
               END
 AS tt, @u_id:=user_id
FROM (select * from app_login_record 
where      TIMESTAMPDIFF(MINUTE, enter_time,  leave_time)  >2
group BY user_id,enter_time) t
ORDER BY user_id,enter_time) tb
where tb.tt = 3

3、第2种方法(没考虑,一天登录2次的情况)、解:

sql 复制代码
select DISTINCT tc.user_id from(
select *,
 if(ta.tg=1 and ta.t_user_id=1,@size:=@size+1,@size:=1) as t_num
from(
select *,lead(left(enter_time,10), 1) over () = 
     DATE_ADD(left(enter_time,10),INTERVAL 1 day) as tg ,
	  lead(user_id, 1) over () = user_id  as t_user_id
	  from app_login_record t , (SELECT @size:=1)r
		where TIMESTAMPDIFF(MINUTE, enter_time,  leave_time)  > 2
ORDER BY t.user_id,t.enter_time) ta)tc
where tc.t_num = 3


-- 怎么定义多个变量,如下
-- (SELECT @old:=null,@size:=1,@name:=null)r

4、第3种方法(这里考虑了,一天登录两次的情况,需要去重)、解:

sql 复制代码
select DISTINCT tw.user_id from(   
select *,
 if(tc.tg=1 and tc.t_user_id=1,@size:=@size+1,@size:=1) as t_num
from (
	select user_id,
	  lead(enter_time_s, 1) over () = 
    DATE_ADD(enter_time_s,INTERVAL 1 day) as tg ,
	  lead(user_id, 1) over () = user_id  as t_user_id
	from( 
    select DISTINCT user_id as user_id , left(enter_time,10) as enter_time_s
 	  from app_login_record t , (SELECT @size:=1)r
		where TIMESTAMPDIFF(MINUTE, enter_time,  leave_time)  > 2
    ORDER BY  t.user_id,enter_time_s)ta)tc)tw
	where tw.t_num = 3
		
		
		
-- 		语法解释1:
-- 		别名不能直接做where和group by后的查询条件,但order by 可以用别名
--    原因是where在select之前执行,所以别名不能直接做where后的查询条件
--    group by 同理。
--    但order by是最后执行,所以可以用别名。


--   语法解释2
--   怎么定义多个变量,如下
--   (SELECT @old:=null,@size:=1,@name:=null)r

5、

6、

7、

8、

9、

10、

11、

相关推荐
IT枫斗者枫哥5 小时前
MyBatis一对多分页:LIMIT 20,为什么凑不齐20个订单?
java·数据库
代码方舟5 小时前
Java数据工程:利用天远全网运营商三要素优化线上实名认证合规体验
java·人工智能
BBmmo5 小时前
我的 Java 学习笔记 · 第 7 篇:泛型与通配符
java
独泪了无痕5 小时前
Hutool之RandomUtil:随机数生成的终极利器
java·后端
花开路口5 小时前
深入理解 Java/Kotlin 协变与逆变
java·kotlin
运行时异常5 小时前
【WMS 仓储系统集成 AI Agent 实战】第 7 讲:Vue3 前端工程化——Token 刷新锁、Markdown 渲染踩坑与权限体系
java
她的男孩5 小时前
开放接口限流从 20 改到 200 还是每分钟 20 次:拆完防重放+幂等+限流,我找到 5 个静默失效的坑
java·后端·架构
Y005 小时前
PostgreSQL 的锁:为什么你的 ALTER TABLE 会卡住,以及怎么查
数据库·postgresql
天天被压力5 小时前
【Python 量化取数指南 #13】Python 把行情落库:sqlite 一键存,回测随用随取
java·人工智能·python
天天被压力5 小时前
【Python 量化取数指南 #14】Python 清洗行情数据:复权停牌对齐,回测不翻车
java·人工智能·python