查找 MySQL 行中存储的日期,在一个查询中查找该日期之后的所有条目

发布于 2024-10-17 00:03:00 字数 346 浏览 7 评论 0原文

我有一个表存储用于限制速率的操作。我想要做的是获取具有“key_action”(启动速率限制时间的操作)的最新行,然后查找该日期之后的所有条目。

目前我能想到的唯一方法是使用两个查询:

  1. SELECTcreated_at FROM actions WHERE key_action=1 ORDER BYcreated_at DESC LIMIT 1

  2. SELECT * FROM 操作 WHEREcreated_at >=(来自查询 1 的created_at)

是否有将这两个查询合并为一个的方法?

I have a table that stores actions for rate-limiting purposes. What I want to do is fetch the newest row that has a 'key_action' (the action that starts the time for rate-limiting) and then find all entries after that date.

The only way I can currently think to do it is with two queries:

  1. SELECT created_at FROM actions WHERE key_action=1 ORDER BY created_at DESC LIMIT 1

  2. SELECT * FROM actions WHERE created_at >= (created_at from query 1)

Is there a was to combine these two queries into one?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

黑色毁心梦 2024-10-24 00:03:00

您可以将查询 1 设为查询 2 的子查询。

SELECT * 
    FROM actions 
    WHERE created_at >= (SELECT MAX(created_at) 
                             FROM actions 
                             WHERE key_action=1)

You can make query 1 a subquery of query 2.

SELECT * 
    FROM actions 
    WHERE created_at >= (SELECT MAX(created_at) 
                             FROM actions 
                             WHERE key_action=1)
夜空下最亮的亮点 2024-10-24 00:03:00

我原以为 @Joe Stefanelli 的答案是正确的,但 WHERE 语句中的子查询不允许限制。从这个解决方法,我整理了这个查询(未经测试)

SELECT * FROM actions 
JOIN (SELECT created_at FROM actions WHERE key_action=1 ORDER BY created_at DESC LIMIT 1) createdActions
WHERE actions.created_at >= createdActions.created_at

I'd have thought @Joe Stefanelli's answer was right, but Limits are not allowed in subqueries in WHERE statement. From this workaround, I put together this query (not tested)

SELECT * FROM actions 
JOIN (SELECT created_at FROM actions WHERE key_action=1 ORDER BY created_at DESC LIMIT 1) createdActions
WHERE actions.created_at >= createdActions.created_at
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文