题目描述

(通过次数4,689 | 提交次数6,957,通过率67.40%)

表: LogInfo
+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| account_id  | int      |
| ip_address  | int      |
| login       | datetime |
| logout      | datetime |
+-------------+----------+
该表是没有主键的,它可能包含重复项。
该表包含有关Leetflex帐户的登录和注销日期的信息。它还包含了该账户用于登录和注销的网络地址的信息。
题目确保每一个注销时间都在登录时间之后。

编写一个SQL查询语句,查找那些应该被禁止的Leetflex帐户编号 account_id 。如果某个帐户在某一时刻从两个不同的网络地址登录了,则这个帐户应该被禁止。
可以以 任何顺序 返回结果。

查询结果格式如下例所示。
示例 1:
输入:
LogInfo table:
+------------+------------+---------------------+---------------------+
| account_id | ip_address | login               | logout              |
+------------+------------+---------------------+---------------------+
| 1          | 1          | 2021-02-01 09:00:00 | 2021-02-01 09:30:00 |
| 1          | 2          | 2021-02-01 08:00:00 | 2021-02-01 11:30:00 |
| 2          | 6          | 2021-02-01 20:30:00 | 2021-02-01 22:00:00 |
| 2          | 7          | 2021-02-02 20:30:00 | 2021-02-02 22:00:00 |
| 3          | 9          | 2021-02-01 16:00:00 | 2021-02-01 16:59:59 |
| 3          | 13         | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |
| 4          | 10         | 2021-02-01 16:00:00 | 2021-02-01 17:00:00 |
| 4          | 11         | 2021-02-01 17:00:00 | 2021-02-01 17:59:59 |
+------------+------------+---------------------+---------------------+
输出:
+------------+
| account_id |
+------------+
| 1          |
| 4          |
+------------+
解释:
Account ID 1 --> 该账户从 "2021-02-01 09:00:00" 到 "2021-02-01 09:30:00" 在两个不同的网络地址(1 and 2)上激活了。它应该被禁止.
Account ID 2 --> 该账户在两个不同的网络地址 (6, 7) 激活了,但在不同的时间上.
Account ID 3 --> 该账户在两个不同的网络地址 (9, 13) 激活了,虽然是同一天,但时间上没有交集.
Account ID 4 --> 该账户从 "2021-02-01 17:00:00" 到 "2021-02-01 17:00:00" 在两个不同的网络地址 (10 and 11)上激活了。它应该被禁止.

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/leetflex-banned-accounts
//测试数据
Create table If Not Exists LogInfo (account_id int, ip_address int, login datetime, logout datetime);

insert into LogInfo (account_id, ip_address, login, logout) values ('1', '1', '2021-02-01 09:00:00', '2021-02-01 09:30:00');
insert into LogInfo (account_id, ip_address, login, logout) values ('1', '2', '2021-02-01 08:00:00', '2021-02-01 11:30:00');
insert into LogInfo (account_id, ip_address, login, logout) values ('2', '6', '2021-02-01 20:30:00', '2021-02-01 22:00:00');
insert into LogInfo (account_id, ip_address, login, logout) values ('2', '7', '2021-02-02 20:30:00', '2021-02-02 22:00:00');
insert into LogInfo (account_id, ip_address, login, logout) values ('3', '9', '2021-02-01 16:00:00', '2021-02-01 16:59:59');
insert into LogInfo (account_id, ip_address, login, logout) values ('3', '13', '2021-02-01 17:00:00', '2021-02-01 17:59:59');
insert into LogInfo (account_id, ip_address, login, logout) values ('4', '10', '2021-02-01 16:00:00', '2021-02-01 17:00:00');
insert into LogInfo (account_id, ip_address, login, logout) values ('4', '11', '2021-02-01 17:00:00', '2021-02-01 17:59:59');

解题思路

这是一个求两个时间区间交叉的题目。
LogInfo表中保存了所有的登录记录。题目要求:查询同一时间在不同IP有过登录的账户。
因为LogInfo表的每一行只保存了一条登录记录,需要求交叉的时间,就需要两两比对。可以使用自关联实现。
当然,比对时,需要限制是同一个账户的不同IP之间。
最后,两个时间区间如何判断是否交叉呢?
对于有交叉的两个时间区间,一定满足一个区间的开始时间,在另一个区间之内。
根据这个特性,用BETWEEN AND可以实现交叉区间的判断。

参考SQL

未特别说明的情况下,参考SQL为基于MySQL8.0实现。
select
    a.account_id
from LogInfo a
inner join LogInfo b
on a.account_id = b.account_id
and a.login between b.login and b.logout
and a.ip_address <> b.ip_address
group by a.account_id;
picture loss