mysql数据库查询某一年内各月份数据,按月份分组

sql语句:

select month(date_no) as month,sum(amount) from table_name 
where year(date_no) = 2017  group by month (date_no) 

表名是table_name, 时间字段是date_no;这里查询的是2017年内 1-12月份每个月 amount这一列的总和;

示例:

表结构大致如下:

select amount,date_no,short_no,worker_id,procedure_id from table_name limit 0,10;

+----------+---------------------+----------+-----------+----------------------------------+
| amount    | date_no                      | short_no | worker_id | procedure_id |
+----------+---------------------+----------+-----------+----------------------------------+
| 3.60         | 2017-12-23 20:37:33 | 111       |        23      |           31 |
| 15000.00 | 2017-12-23 20:43:12 | 8806     | NULL       |           41 |
| 600.00     | 2017-12-23 20:46:42 | 8826     |        24     |           41 |
| 600.00     | 2017-12-23 20:46:42 | 8826     |        25     |           41 |
| 120.00     | 2017-12-23 20:53:22 | 8826     | NULL       |           40 |
| 120.00     | 2017-12-23 21:29:11 | 12         |        27     |           34 |
| 48.00       | 2017-12-23 21:32:34 | 12         | NULL       |           35 |
| 12.00       | 2017-12-23 21:35:49 | 12         |        30     |           38 |
| 1.00         | 2017-12-23 21:51:33 | 1           |        23     |           31 |
| 12.00       | 2017-12-23 22:13:40 | 1           |        30     |           37 |
+----------+---------------------+----------+-----------+--------------------------+

查询2018年各个月份的统计数据:

select month(date_no) as month,sum(amount) from table_name
where year(date_no) = 2018 and  group by month (date_no);

+-------+-------------+
| month | sum(amount) |
+-------+-------------+
|     1 | 161753.09   |
|     2 | 1020.00     |
|     3 | 34422.80    |
|     4 | 10498.80    |
|     5 | 1670.40     |
|     6 | 1182.00     |
|     7 | 195.20      |
|     8 | 161404.65   |
|     9 | 117190.12   |
|    10 | 138.20      |
+-------+-------------+

你可能感兴趣的:(mysql,数据库,mysql,sql)