使用 MySql 查询帮助 - 包含某个列的分组最大值的行 [英] Help with a MySql Query - The Rows Holding the Group-wise Maximum of a Certain Column

查看:37
本文介绍了使用 MySql 查询帮助 - 包含某个列的分组最大值的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助返回此查询的相关结果.我有一张桌子,上面有三列.trans_date、trans_amount 和 user_id

I need help returning a relevant result for this query. I have one table that I am hitting with three columns. trans_date, trans_amount and user_id

我想确定的是这个.对于给定的 user_id,上次 trans_date 是什么时候,trans_amount 是多少.

what I am trying to determine is this. For a given user_id when was the last trans_date and what was the trans_amount.

我无法返回正确的 transaction_amount.到目前为止,这是我的代码.它返回了正确的日期,但金额不正确

I'm having trouble returning the correct transaction_amount. Here is my code so far. It's returning the correct date but the amount is not right

select user_id, trans_date, trans_credit
from table
WHERE trans_credit = 
(select max(trans_date) from inclick_account_act as f 
 where f.user_id = table.user_id);

提前致谢

推荐答案

如果我理解正确,您只想获得所有用户的最新交易.

If I understand you correctly you just want to get the most recent transaction for all users.

  SELECT user_id, trans_date, trans_credit
    FROM `table`
GROUP BY user_id
ORDER BY trans_date DESC;

这篇关于使用 MySql 查询帮助 - 包含某个列的分组最大值的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆