我有两个表1是帐户,另一种是account_spend_history灰分帐户是父/主表和嘘一张小桌子。并与账户OneToMany有关系(账户表id是ash中的一个外键,如account_id)。请看图片
现在我需要获取account.id,总花费(这是amount_spend与相同的account_id的总和)和最后花费(是在灰表中插入的最后一条记录,并且account_id即amount_spend值与MAX(ash.id))对应的,即
id | spend_total | last_spend --------------------------------- 1 | 30 | 18 2 | 280 | 120 3 | 20 | 20
SELECT a.id, SUM(ash.amount_spend) AS spend_total FROM accounts as a INNER JOIN account_spend_history AS ash ON ash.account_id = a.id GROUP BY a.id
即时得到ash.amount支出的account.id和总和,但我还需要最后的支出。如何获得?
感谢。
谢谢@sgeddes,它正在为我,我得到了期望的输出。 –