# Write your MySQL query statement below
1939. 主动请求确认消息的用户
Table: Signups
+----------------+----------+ | Column Name | Type | +----------------+----------+ | user_id | int | | time_stamp | datetime | +----------------+----------+ user_id is the primary key for this table. 每行包含有关 ID 为 user_id 的用户的注册时间的信息。
Table: Confirmations
+----------------+----------+ | Column Name | Type | +----------------+----------+ | user_id | int | | time_stamp | datetime | | action | ENUM | +----------------+----------+ (user_id, time_stamp) is the primary key for this table. user_id is a foreign key with a reference to the Signups table. action is an ENUM of the type ('confirmed', 'timeout') 此表的每一行都表示 ID 为 user_id 的用户在 time_stamp 请求了确认消息,并且该确认消息已被确认('confirmed')或已过期('timeout')。
编写 SQL 查询以查找在 24 小时窗口内两次请求确认消息的用户的 ID。 两个正好相隔 24 小时的消息被认为是在窗口内。 该操作不会影响答案,只会影响请求时间。
以任意顺序返回结果表。
查询结果格式如下例:
Signups table: +---------+---------------------+ | user_id | time_stamp | +---------+---------------------+ | 3 | 2020-03-21 10:16:13 | | 7 | 2020-01-04 13:57:59 | | 2 | 2020-07-29 23:09:44 | | 6 | 2020-12-09 10:39:37 | +---------+---------------------+ Confirmations table: +---------+---------------------+-----------+ | user_id | time_stamp | action | +---------+---------------------+-----------+ | 3 | 2021-01-06 03:30:46 | timeout | | 3 | 2021-01-06 03:37:45 | timeout | | 7 | 2021-06-12 11:57:29 | confirmed | | 7 | 2021-06-13 11:57:30 | confirmed | | 2 | 2021-01-22 00:00:00 | confirmed | | 2 | 2021-01-23 00:00:00 | timeout | | 6 | 2021-10-23 14:14:14 | confirmed | | 6 | 2021-10-24 14:14:13 | timeout | +---------+---------------------+-----------+ Result table +---------+ | user_id | +---------+ | 2 | | 3 | | 6 | +---------+
用户 2 在彼此恰好 24 小时内请求了两条消息,因此我们将它们包括在内。 用户 3 在 6 分 59 秒内请求了两条消息,因此我们将它们包括在内。 用户 6 在 23 小时 59 分 59 秒内请求了两条消息,因此我们将它们包括在内。 用户 7 在 24 小时 1 秒内请求了两条消息,因此我们将它们从答案中排除。
原站题解
mysql 解法, 执行用时: 512 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 16:53:40
# Write your MySQL query statement below select distinct a.user_id from Confirmations as a inner join Confirmations as b on a.user_id = b.user_id where a.time_stamp < b.time_stamp and timestampdiff(second, a.time_stamp, b.time_stamp) <= 24 * 60 * 60
mysql 解法, 执行用时: 469 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 16:53:20
-- 第一次使用 timestampdiff 函数,可以指定两个日期做差值的粒度,hour/second/minate等 WITH tmp AS( SELECT user_id, time_stamp, lead(time_stamp,1,'9999-99-99 00:00:00') over(partition by user_id order by time_stamp) as next_time FROM Confirmations ) SELECT distinct user_id FROM tmp WHERE next_time != '9999-99-99 00:00:00' AND ABS(TIMESTAMPDIFF(SECOND, next_time, time_stamp)) <= 86400
mysql 解法, 执行用时: 500 ms, 内存消耗: 0 B, 提交时间: 2023-10-15 16:53:00
select distinct c1.user_id from Confirmations c1 inner join Confirmations c2 on c1.time_stamp<c2.time_stamp and c1.user_id=c2.user_id and TimeStampDiff(second,c1.time_stamp,c2.time_stamp)<=86400