This question already has answers here:
MySQL cumulative sum grouped by date
                                
                                    (2个答案)
                                
                        
                                3年前关闭。
            
                    
总结每月的每一天

这是我叫付款的桌子

amount  | date_added
===================================
30.00           |2016-12-05 20:48:13
-10             |2016-12-10 17:39:45
-55.30          |2016-12-10 17:55:18
78.00           |2016-12-11 14:50:50
-20.00          |2016-12-11 15:50:50
-30.00          |2016-12-22 01:47:22
300.00          |2016-12-23 01:01:36
-40             |2016-12-24 02:10:00


我尝试了以下查询,但它给出了每天的总和。

SELECT SUM(`amount`) as total_amount, `date_added`  FROM `payment` where (`payment`.`date_added` between '2016-12-1'  and '2016-12-30') GROUP BY  DATE(date_added)


结果是

total_amount    | date_added
===================================
30.00       |2016-12-05 20:48:13
-65.30      |2016-12-10 17:39:45
58.00       |2016-12-11 14:50:50
-30.00      |2016-12-22 01:47:22
300.00      |2016-12-23 01:01:36
-40.00      |2016-12-24 02:10:00


我想要的是总结每月的每一天。不是每天的总和。

我希望使用Mysql获得以下结果。

total_amount    | date_added
===================================
30.00       |2016-12-05 20:48:13
-35.30      |2016-12-10 17:39:45
22.70       |2016-12-11 14:50:50
-7.30       |2016-12-22 01:47:22
292.70      |2016-12-23 01:01:36
252.70      |2016-12-24 02:10:00

最佳答案

测试此代码。在第二张桌子之后。然后,您可以编写最佳查询。在这里表t是第二个表

SELECT (SELECT SUM(x.count)
            FROM TABLE x
           WHERE x.id <= t.total_amount) AS total_amount
    FROM TABLE t
ORDER BY t.total_amount

10-06 03:08