SQL高级用法

1、查找连续三天登录的用户

create table people(
user_id int,
visit_time datetime
);
insert into people(user_id,visit_time) values
(1001,'2023-10-01 08:01:23'),
(1001,'2023-10-01 08:01:23'),
(1002,'2023-10-01 08:01:23'),
(1003,'2023-10-01 08:01:23'),
(1002,'2023-10-02 08:01:23'),
(1004,'2023-10-02 08:01:23'),
(1004,'2023-10-02 08:01:23'),
(1005,'2023-10-02 08:01:23'),
(1002,'2023-10-03 08:01:23'),
(1003,'2023-10-03 08:01:23')

#去除用户一天多次登录
select distinct user_id,
date(visit_time) as dt
from people;
#用户id进行分组

select user_id,COUNT(dt) from (
select distinct user_id, date(visit_time) as dt
from people
) a GROUP BY user_id;

你可能感兴趣的:(sql,数据库,mysql)