
[英]i want to send unique records for every user who logs in from a mysql database
[英]I want to find a user who tried 3 logins in 5 minutes. in mysql 5.7
这个数据在mysql5.7
我想找到一个在 5 分钟内尝试 3 次登录的用户。
这可能与 mysql 查询吗?
+----+--------+----------------------+
| id | type | date |
+----+--------+----------------------+
| a | login | 2019-10-17 10:01:00 |
| a | login | 2019-10-17 10:07:00* |
| a | login | 2019-10-17 10:10:00* |
| b | login | 2019-10-17 10:00:00 |
| b | login | 2019-10-17 10:10:00 |
| b | logout | 2019-10-17 10:20:00 |
| a | logout | 2019-10-17 10:11:00* |
| a | login | 2019-10-17 10:11:30* |
| b | login | 2019-10-17 10:10:00 |
| c | login | 2019-10-17 10:08:00 |
| c | login | 2019-10-17 10:27:00* |
| b | logout | 2019-10-17 10:30:00 |
| c | login | 2019-10-17 10:30:00* |
| c | login | 2019-10-17 10:31:00* |
+----+--------+----------------------+
我想得到以下结果:
+----+--------+-------+
| id | type | count |
+----+--------+-------+
| a | login | 4 |
| c | login | 3 |
+----+--------+-------+
这是表结构和数据示例;
CREATE TABLE `a` (
`id` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`type` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Dumping data for table `a`
--
INSERT INTO `a` VALUES
('a','login','2019-10-17 10:01:00'),
('a','login','2019-10-17 10:07:00'),
('a','login','2019-10-17 10:10:00'),
('b','login','2019-10-17 10:00:00'),
('b','login','2019-10-17 10:10:00'),
('b','logout','2019-10-17 10:20:00'),
('a','logout','2019-10-17 10:11:00'),
('a','login','2019-10-17 10:11:30'),
('b','login','2019-10-17 10:10:00'),
('c','login','2019-10-17 10:08:00'),
('c','login','2019-10-17 10:27:00'),
('b','logout','2019-10-17 10:30:00'),
('c','login','2019-10-17 10:30:00'),
('c','login','2019-10-17 10:31:00');
使用 group by id 和 count(*) with
完整查询
select t1.id, t1.type, count(*) ct
from a t1
left join a t2 on t2.id = t1.id and t2.date >= date_add(t1.date, interval 5 minute)
where t2.type = 'login' and t1.type = 'login'
group by t1.id, t1.type
having count(1) >= 3
我不知道计数是多少。 但你可以这样做:
select distinct id
from t
where t.type = 'login' and
(select count(*)
from t t2
where t2.type = 'login' and
t2.date >= t.date and
t2.date < t.date + interval 5 minute
) >= 3;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.