sql连续登录

news/2024/10/21 6:04:07/

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 * ,CASEWHEN 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+1WHEN @old:=enter_time THEN @size:=1 ENDAS 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_idfrom app_login_record t , (SELECT @size:=1)rwhere 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_idfrom( select DISTINCT user_id as user_id , left(enter_time,10) as enter_time_sfrom app_login_record t , (SELECT @size:=1)rwhere TIMESTAMPDIFF(MINUTE, enter_time,  leave_time)  > 2ORDER BY  t.user_id,enter_time_s)ta)tc)twwhere 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、


http://www.ppmy.cn/news/1447689.html

相关文章

MongoDB聚合运算符:$sqrt

MongoDB聚合运算符&#xff1a;$sqrt 文章目录 MongoDB聚合运算符&#xff1a;$sqrt语法使用举例 $sqrt聚合运算符返回数值的平方根&#xff0c;数值必须为正数&#xff0c;返回值为双精度数。 语法 { $sqrt: <number> }<expression>为可解析为非负数的表达式。 …

企微社群群发消息:强化社群互动与营销新策略

在数字化营销日益盛行的今天&#xff0c;企业微信社群已成为企业与用户之间沟通互动的重要桥梁。而企微社群群发消息功能&#xff0c;更是这一桥梁上的重要纽带&#xff0c;它能够帮助企业高效、精准地传递信息&#xff0c;强化社群互动&#xff0c;提升营销效果。本文将详细介…

ios微信小程序禁用下拉上拉

第一步&#xff1a; page.json配置页面的"navigationStyle":"custom"属性&#xff0c;禁止页面滑动 "navigationStyle":"custom" 第二步&#xff1a; 页面里面使用scroll-view包裹内容&#xff0c;内容可以内部滑动 <view class&…

MySQL---创建数据库与表

一.建立数据库 mysql> create database db_classes-> default charset utf8mb4; Query OK, 1 row affected (0.03 sec)mysql> show databases; -------------------- | Database | -------------------- | db_classes | | information_schema | |…

实习面试算法准备之图论

这里写目录标题 1 基础内容1.1 图的表示1.2图的遍历 2 例题2.1 所有可能的路径2.2 课程表&#xff08;环检测算法&#xff09;2.2.1 环检测算法 DFS版2.2.2 环检测算法 BFS版 2.3 课程表 II &#xff08;拓扑排序算法&#xff09;2.3.1 拓扑排序 DFS版 1 基础内容 图没啥高深的…

【Jenkins】持续集成与交付 (五):Jenkins用户权限管理

🟣【Jenkins】持续集成与交付 (五):Jenkins用户权限管理 1、安装插件(Role-based Authorization Strategy)2、开启权限全局安全配置3、创建角色4、创建用户5、给用户分配角色6、测试权限💖The Begin💖点点关注,收藏不迷路💖 1、安装插件(Role-based Authorization …

Babylon.js的优势

Babylon.js 是一个为Web开发人员设计的强大的3D引擎&#xff0c;它具有以下优势&#xff0c;这些优势使得Babylon.js成为创建引人入胜的3D Web体验的强大工具。北京木奇移动技术有限公司&#xff0c;专业的软件外包开发公司&#xff0c;欢迎交流合作。 1.易于学习&#xff1a; …

深入浅出区块链技术:原理、应用与挑战

区块链技术是一种分布式数据库技术&#xff0c;其核心在于提供一个去中心化、不可篡改的数据记录系统。以下是区块链技术的原理、应用和面临的挑战的详细解析&#xff1a; ### 原理 1. **去中心化**&#xff1a;区块链技术不依赖于中央控制点&#xff0c;而是通过网络上的多个…