這個是用mid()函數 截取字符串
mysql> select count(*) as '數量',mid(time,1,7) as month from my_add where time between '2015-01-01 00:00:00' and '2016-01-01 00:00:00' group by month;
+------+---------+
| 數量 | month |
+------+---------+
| 2 | 2015-01 |
| 1 | 2015-08 |
| 1 | 2015-09 |
| 1 | 2015-10 |
| 1 | 2015-11 |
| 2 | 2015-12 |
+------+---------+
這是另一種方法date_format()
mysql> select count(*) as '數量',date_format(time,'%m') as month from my_add group by month;
+------+-------+
| 數量 | month |
+------+-------+
| 1 | NULL |
| 3 | 01 |
| 1 | 08 |
| 1 | 09 |
| 1 | 10 |
| 1 | 11 |
| 2 | 12 |
+------+-------+
會了 感覺挺簡單 不會之前 真的是怎么都搞不定 總之搞定了 希望遇到這種問題的人能看到它解決把