MySQL 最后一个操作不是“停止”;

发布于 2024-10-21 08:59:20 字数 216 浏览 5 评论 0原文

我有一个表存储用户名、操作(停止、开始、暂停)和操作时间戳。

因此,这些列是 useridnameactiontimestamp

如何获取活跃用户。即最后一个操作不是“停止”的用户。

更感谢您帮助我构建查询(而不是发布有效的查询)和/或为我指明正确的方向。

I have a table that stores users name, action(stop,start,pause) and timestamp for action.

Thus the columns are userid, name, action and timestamp

How do I get the active users. I.e. users who's last action was not "stop".

More appreciation for helping me construct the query (instead of posting one that works) and/or for pointing me in the right direction.

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

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

发布评论

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

评论(3

清泪尽 2024-10-28 08:59:20

选择action字段的最后一个值意味着使用max(timestamp)查询字段进行聚合查询。在这种情况下,您将无法从同一记录中选择 timestamp = max(timestamp) 的其他字段,因为不允许在没有聚合函数的情况下聚合查询字段。

因此,您需要一种子查询来选择用户的上次操作时间。另外,您只能通过timestampuserid 本身来加入

现在,进行查询:

select actions.userid, actions.action
from actions
  inner join (select max(timestamp) timestamp, userid from actions group by userid) lastActions
    on actions.userid = lastActions.userid and actions.timestamp = lastActions.timestamp
where actions.action != 'stop'

Selecting a last value of action field means aggregate query with max(timestamp) query field. In this case you won't be able to pick other fields from the same record for which timestamp = max(timestamp), because it's not allowed to aggregate-query fields without an aggregate function.

So you need a kind of subquery to pick user's last action time. Plus, you will only be able to join with it by timestamp and userid itself.

Now, on to query:

select actions.userid, actions.action
from actions
  inner join (select max(timestamp) timestamp, userid from actions group by userid) lastActions
    on actions.userid = lastActions.userid and actions.timestamp = lastActions.timestamp
where actions.action != 'stop'
满意归宿 2024-10-28 08:59:20

您应该创建一个选择语句,按userid对所有数据进行分组,并按最大timestamp对其进行排序 - 这将为您提供用户的最后一个操作(带有每组最大的时间戳)。

现在剩下要做的就是过滤掉最后没有停止操作的组。

  • HINT - 在MySQL中查找HAVING语句以获取过滤部分

you should create a select statement that groups all the data by userid, and order it by the maximum timestamp - this will give you the last action for a user (the action with the biggest timestamp per group).

all is left to do now is just filter out the groups that do not have a stop action as a last action.

  • HINT - lookup the HAVING statement in MySQL for the filtering part
能否归途做我良人 2024-10-28 08:59:20

我保证困难会出现,因为用户可能有很多操作:

1,mjuaji,stop,00002323

1,mjuaji,start,00002324

1,mjuaji,pause ,00002325

嗯,为您指明正确的方向:

您可以首先考虑如何首先选择每个用户的最后一个操作
尝试分组、聚合运算符(最大、最小...),

然后查看最后一个操作是否不是“停止”。

I assure the difficulty comes in because a user may have many actions:

1,mjuaji,stop,00002323

1,mjuaji,start,00002324

1,mjuaji,pause,00002325

um, to point you to the right direction:

you could start by thinking about how to select the last action of each user first.
try grouping, aggregate operators (max,min...)

then see if the last action is not "stop".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文