mysql - MySQL - 获取给定列值列表的最新记录
问题描述
我有以下表结构:
请求表:
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
推荐阅读
- graphql - 预期类型变量是使用 Apollo 的对象
- datetime - 如何在plsql中将晚上8点转换为20小时?
- python - Conda 与操作系统冲突
- powershell - powershell 和 WCF 服务的奇怪之处
- javascript - 带有 React 的条件代码片段显示了这两种情况
- r - R中使用emmeans和geepack的每组边际均值和置信水平
- java - 如何彻底测试使用接口的类
- sql - 如何最小化此 Oracle SQL 中使用的字符数?
- ios - 通过检查属性比较集合中的元素并删除
- python - 为什么 print、sys.stdout.write 和 os.write 不在终端打印任何东西?