Person表定義以下:sql
create table person(id int primary key auto_increment, birthday datetime);
Person 數據以下:blog
MariaDB [test]> select * from person; +----+---------------------+ | id | birthday | +----+---------------------+ | 1 | 2019-04-15 15:00:00 | | 2 | 2019-04-16 15:00:00 | | 3 | 2019-04-17 15:00:00 | | 4 | 2019-04-18 15:00:00 | | 5 | 2019-03-18 15:00:00 | | 6 | 2019-03-17 15:00:00 | | 7 | 2018-02-17 15:00:00 | | 8 | 2019-02-16 15:00:00 |
按月統計每個月生日人數:rem
MariaDB [test]> select DATE_FORMAT(birthday, "%m") as month, count(1) as num from person group by DATE_FORMAT(birthday, "%m"); +-------+-----+ | month | num | +-------+-----+ | 02 | 2 | | 03 | 2 | | 04 | 4 | +-------+-----+ 3 rows in set (0.00 sec)