【发布时间】:2016-03-17 03:50:26
【问题描述】:
我正在尝试创建一个查询以查找一个线程,该线程具有至少一条日期小于 20 分钟的记录
这是一个如下所示的心跳表:
+--------------+-----------+---------------------+---------------------+
| heartbeat_id | server_id | thread | last_checkin |
+--------------+-----------+---------------------+---------------------+
| 3 | 133 | EscalationMonitor | 2016-03-16 23:46:07 |
| 7 | 133 | EmailMonitor | 2016-03-16 23:47:31 |
| 11 | 133 | TableMonitor | 2016-03-16 23:42:49 |
| 15 | 133 | NotificationMonitor | 2016-03-16 23:46:30 |
| 19 | 127 | EmailMonitor | 2016-03-16 23:47:21 |
| 23 | 127 | TableMonitor | 2016-03-16 23:46:11 |
| 27 | 127 | EscalationMonitor | 2016-03-16 23:47:58 |
| 31 | 127 | NotificationMonitor | 2016-03-16 23:41:10 |
| 35 | 123 | EmailMonitor | 2016-03-16 23:47:59 |
| 39 | 123 | TableMonitor | 2016-03-16 23:43:10 |
| 43 | 123 | EscalationMonitor | 2016-03-16 23:46:26 |
| 47 | 123 | NotificationMonitor | 2016-03-16 23:46:47 |
+--------------+-----------+---------------------+---------------------+
这是我所在的位置,但这会搜索至少一条超过 20 分钟的记录。
SELECT * FROM heartbeat h WHERE exists
(
select * from heartbeat h2
where last_checkin < date_add(now(), INTERVAL - 20 MINUTE)
)
group by thread
【问题讨论】:
-
从
INTERVAL - 20 MINUTE中删除- -
这意味着 last_checkin
-
不,它将早于创建日期后的
20分钟 -
假设您的记录已添加到您的表中,它们必须具有创建日期,因此这将仅检查自 20 分钟前插入的记录
-
现在可以说是
23:59。并以EmailMonitor为例。您当前的查询为您提供7,133,EmailMonitor, 2016-03-16 23:47:31。你对EmainMonitor有什么期望,为什么?
标签: mysql sql group-by exists having