我有这张桌子

| box    | indate     |
 ----------------------
| 500    | 2015-01-19 |
 ----------------------
|200     | 2015-01-17 |
 ---------------------
| 1000   | 2015-03-19 |
 ---------------------
| 500    | 2015-03-7  |
 ---------------------


我想要这个仓库

| Year - Month| Total In box |
------------------------------
| 2015- 01    | 700          |
------------------------------
| 2015-03     | 1500         |
------------------------------


我也想在php中显示年度报告。

这是我到目前为止尝试过的:

SELECT DATE_FORMAT(indate,'%Y-%m'), SUM(box)
FROM in GROUP BY YEAR(indate), MONTH(indate)

最佳答案

你很亲密
只需修改查询并使用SUM(box)代替COUNT(box)
像这样:

SELECT DATE_FORMAT(indate,'%Y-%m') as indate, SUM(box)
FROM Table1 GROUP BY YEAR(indate), MONTH(indate);


对于Year明智的报告,只需在Group By中不要包含MONTH

SELECT DATE_FORMAT(indate,'%Y-%m') as indate, SUM(box)
FROM Table1 GROUP BY YEAR(indate);

10-08 13:32