用户的SQL组付款并跟踪上次date_paid?

时间:2013-06-07 21:26:46

标签: mysql sql

好的,我有一张用户表和一张付款表。每笔付款都有date_paid和amount_paid。我想按支付金额的总和对付款进行分组,但我还想保留最后一个date_paid的字段。这可能吗?或者我是否必须在用户表中存储last_date_paid列?

1 个答案:

答案 0 :(得分:3)

SELECT id, SUM(amount_paid), MAX(date_paid)
FROM payments
GROUP BY id