MySQL - 获取给定列值列表的最新记录

标签 mysql sql hibernate

我有以下表结构:

请求表:

 id  insret_time             account id
 ------------------------------------
 1  2018-04-05 08:06:23       abc
 2  2018-09-03 08:14:45       abc
 3  2018-08-13 09:23:34       xyz
 4  2018-08-04 09:25:37       def
 5  2018-08-24 11:45:37       def

我需要找到帐户 ID abc 和 def 的最新记录。我不关心xyz。

我尝试使用 group by 和 inner join 方法获取结果,但没有成功地将结果限制为我关心的用户列表。 请指教

更新: 感谢大家的反馈。欣赏它!我需要整行作为输出。自从它自动递增以来,我使用 id 列而不是时间戳来获取最新记录这是我最终想出的,它给了我需要的输出:

select t.* FROM table t
join (select max(table.id) as maxNum from table 
where account_id in ('abc','def') group by account_id) tm on t.id = tm.maxNum;

最佳答案

我想这就是你要找的

  select account_id,max(insret_time)
  from table where account_id in ('abc', 'def')
  group by account_id

关于MySQL - 获取给定列值列表的最新记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53964932/

相关文章:

java - Hibernate envers 获取修订日志

mysql - 错误1005无法创建表user.EMPLOYEE和user.STORE(errno 150)

PHP MySql update() 带数组

javascript - Node.js mysql循环仅显示最后一个条目

sql - 在 T-SQL 中使用环境变量

mysql - 如何解决使用 hibernate 5 和 mysql 出现 HHH000346 错误?

mysql - LIKE 语句未在查询中找到全部

python - Django:批量操作

sql - 查询非日期格式的特定属性的最新日期

hibernate - Spring Boot 2/Kotlin/JDK9 - 由 : java. lang.ClassNotFoundException : javax. transaction.SystemException 引起