MySQL 最后一个操作不是 'stop'

标签 mysql timestamp

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

因此,列是 useridnameactiontimestamp

如何获得活跃用户。 IE。最后操作不是“停止”的用户。

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

最佳答案

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

因此,您需要一种子查询来选择用户的上次操作时间。另外,您只能 join与它 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'

关于MySQL 最后一个操作不是 'stop',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5260590/

相关文章:

php - 如何调整sql查询中的时间戳?

php - 将 phpmyadmin 连接到 mysql workbench 中创建的本地数据库

mysql - 选择数组内爆中映射 id 的位置

php - 自动刷新显示表,无需重新加载页面

mysql - 当使用 mySQL 和 PrestoDB 的时间戳是 big int 时,有没有办法在 AWS Athena 中拉取数据范围?

PHP:如何从模式 H:i (17:45) 创建时间戳?

JAVA new Timestamp() 使以毫秒为单位的时间以零结尾时结尾零消失

php - 从 MySQL 表自动检查 URL

php - 使用 mysql 和 php

postgresql - 如何从 PostgreSQL 中的 timestamp(0) 舍入毫秒值?