sql连续登录

server/2024/9/23 1:02:21/

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/server/27495.html

相关文章

粤嵌gec6818开发板-播放视频、音频文件(管道文件控制)

前段时间做了一个项目,用到了linux环境下gec6818开发板播放视频、音频文件,在这里给大家分享一下。 这里使用的方法是利用mplayer播放器进行播放,首先先给开发板装上mplayer播放器,这里就不详细说明了。 我用的是管道文件来控制视…

【综述】多核处理器芯片

文章目录 前言 Infineon处理器 AURIX™系列 TC399XX-256F300S 典型应用 开发工具 参考资料 前言 见《【综述】DSP处理器芯片》 Infineon处理器 AURIX™系列,基于TriCore内核,用于汽车和工业领域。 XMC™系列,基于ARM Cortex-M内核&…

搭建vue3组件库(一): Monorepo架构搭建

文章目录 1. 以 pnpm 构建 monorepo1.1 全局安装 pnpm1.2 配置 pnpm 的 monorepo 工作区1.3 仓库项目内的包相互调用1.4 TypeScript 初始化配置文件 2. 通用配置文件2.1 添加 .editorconfig 编辑器格式配置文件2.2 添加 .gitignore git 忽略文件2.3 添加 .npmrc npm配置文件2.4…

数据结构(八)——排序

八、排序 8.1 排序的基本概念 排序(Sort),就是重新排列表中的元素,使表少的元素满足按关键字有序的过程。 输入∶n个记录R1,R2...., Rn,对应的关键字为k1, k2,... , kn 输出:输入序列的一个重排R1,R2....,Rn,使得有k1≤k2≤...≤…

Java设计模式 _结构型模式_过滤器模式

一、过滤器模式 1、过滤器模式 过滤器模式(Filter Pattern)是这一种结构型设计模式。过滤器,顾名思义,就是对一组数据进行过滤,从而最终获取到我们预期的数据。 2、实现思路 (1)、定义过滤器的…

CUDA内存模型

核函数性能并不只与线程束的执行有关。 CUDA内存模型概述 GPU和CPU内存模型的主要区别是,CUDA编程模型能将内存层次结构更好地呈现给用户,能让我们显示的控制它的行为。 对程序员来说,一般有两种类型的存储器: 可编程的&#x…

Android 14设置android:importantForAutofill=“no“无效

密码输入框EditText不希望弹出Google的是否保存密码弹出框, 直接设置了android:importantForAutofill"no", android:inputType"textPassword|textNoSuggestions"在安卓12手机上有效,但是在安卓14上面就不行了&#xff0…

PotatoPie 4.0 实验教程(33) —— FPGA实现摄像头视频图像叠加

链接直达 https://item.taobao.com/item.htm?ftt&id776516984361 什么是视频水印? 视频水印就是图像叠加,跟画中画,或者是OSD是一样的原理,都是在视频的行场数据流上进行替换操作,比如叠加可以直接用水印图的数…