总结一下每月的每一天
这是我的名为payment
的表格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
答案 0 :(得分:0)
测试此代码。在第二张桌子之后然后你可以写出最好的查询。在这里,表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