java
复制代码
CREATE TABLE `user_login` (
`id` int NOT NULL AUTO_INCREMENT,
`user_id` int DEFAULT NULL,
`login_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
#插入数据
INSERT INTO study.user_login (user_id,login_date) VALUES
(100,'2023-10-01 00:00:00.0')
,(100,'2023-10-02 00:00:00.0')
,(100,'2023-10-03 00:00:00.0')
,(100,'2023-10-04 00:00:00.0')
,(100,'2023-10-05 00:00:00.0')
,(100,'2023-10-06 00:00:00.0')
,(100,'2023-10-07 00:00:00.0')
,(100,'2023-10-07 00:00:00.0')
,(200,'2023-10-20 00:00:00.0')
,(200,'2023-10-22 00:00:00.0')
;
INSERT INTO study.user_login (user_id,login_date) VALUES
(200,'2023-10-23 00:00:00.0')
;
sql
复制代码
select
log_date,
user_id from
(
select
*,
date_sub(log_date,interval rank_num day) real_date
from
(
select
*,
row_number() over (partition by user_id order by log_date ) as rank_num
from
(
select
distinct date(login_date) as log_date ,user_id
from study.user_login
) a
) b
) c
group by real_date, user_id HAVING COUNT(*) >=7