返回介绍

solution / 1700-1799 / 1747.Leetflex Banned Accounts / README_EN

发布于 2024-06-17 01:03:15 字数 3635 浏览 0 评论 0 收藏 0

1747. Leetflex Banned Accounts

中文文档

Description

Table: LogInfo

+-------------+----------+
| Column Name | Type   |
+-------------+----------+
| account_id  | int    |
| ip_address  | int    |
| login     | datetime |
| logout    | datetime |
+-------------+----------+
This table may contain duplicate rows.
The table contains information about the login and logout dates of Leetflex accounts. It also contains the IP address from which the account was logged in and out.
It is guaranteed that the logout time is after the login time.

 

Write a solution to find the account_id of the accounts that should be banned from Leetflex. An account should be banned if it was logged in at some moment from two different IP addresses.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
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 |
+------------+------------+---------------------+---------------------+
Output: 
+------------+
| account_id |
+------------+
| 1      |
| 4      |
+------------+
Explanation: 
Account ID 1 --> The account was active from "2021-02-01 09:00:00" to "2021-02-01 09:30:00" with two different IP addresses (1 and 2). It should be banned.
Account ID 2 --> The account was active from two different addresses (6, 7) but in two different times.
Account ID 3 --> The account was active from two different addresses (9, 13) on the same day but they do not intersect at any moment.
Account ID 4 --> The account was active from "2021-02-01 17:00:00" to "2021-02-01 17:00:00" with two different IP addresses (10 and 11). It should be banned.

Solutions

Solution 1: Self-Join

We can use a self-join to find out the cases where each account logs in from different IP addresses on the same day. The conditions for joining are:

  • The account numbers are the same.
  • The IP addresses are different.
  • The login time of one record is within the login-logout time range of another record.
# Write your MySQL query statement below
SELECT DISTINCT
  a.account_id
FROM
  LogInfo AS a
  JOIN LogInfo AS b
    ON a.account_id = b.account_id
    AND a.ip_address != b.ip_address
    AND a.login BETWEEN b.login AND b.logout;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文