如何使用mysql选择组中的LAST记录

我正在尝试在sql中选择group by中的记录的LAST。 就我而言,我使用此代码

SELECT c_id,Max(transaction_num),Max(trans_date) trans_date,doc_type,amount as balance
  from tbl_ledger 
 where doc_type = 'B' 
 group 
    by c_id

并且所选列的数据是这个

如何使用mysql选择组中的LAST记录

所以我基本上试图获得最后的余额。如何选择sql中的最后一个金额?

整个表的内容在这里:tbl_ledger

如何使用mysql选择组中的LAST记录

我的输出得到第一个余额:

如何使用mysql选择组中的LAST记录

xingyuechen 回答:如何使用mysql选择组中的LAST记录

以下查询为您提供了每个trans_date具有最新c_id的记录:

select t.*
from tbl_ledger t
where 
    doc_type = 'B'
    and trans_date = (
        select max(t1.trans_date)
        from tbl_ledger t1
        where t1.doc_type = 'B' and t1.c_id = t.c_id
    )
本文链接:https://www.f2er.com/2992559.html

大家都在问