mysql 查詢一年中每個月份的數據量


這個是用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    |

+------+-------+

會了 感覺挺簡單  不會之前 真的是怎么都搞不定    總之搞定了   希望遇到這種問題的人能看到它解決把


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM