Home >Database >Mysql Tutorial >How to query by month in mysql
Mysql group query method by month: format the date value according to the format string, the syntax is [DATE_FORMAT(date,format)], where [%M] is the name of the month.
Mysql group query method by month:
select DATE_FORMAT(createtime,'%Y-%m'),count(*) from test where user =8 group by DATE_FORMAT(createtime,'%Y-%m');//按月统计数据 select DATE_FORMAT(createtime,'%Y-%m-%d'),count(*) from test where user =8 group by DATE_FORMAT(createtime,'%Y-%m-%d');//按天统计数据
+---------------------------------+----------+ | DATE_FORMAT(createtime,'%Y-%m') | count(*) | +---------------------------------+----------+ | 2015-05 | 1 | | 2015-06 | 1 | | 2015-07 | 5 | | 2015-08 | 1 | +---------------------------------+----------+ 4 rows in set
DATE_FORMAT(date,format)
Format the date value according to the format string. The following modifiers can be used in the format string:
%M month name (January......December)
%W week name (Sunday...Saturday)
%D The day of the month with an English prefix (1st, 2nd, 3rd, etc.)
%Y year, number, 4 digits
##%p AM or PM
%w The number of days in a week (0=Sunday...6=Saturday)
%U The number of days in a week (0...52), where Sunday is the first day of the week
%u Week (0...52), where Monday is the first day of the week
%% A literal "%".
mysql tutorial(Video)
The above is the detailed content of How to query by month in mysql. For more information, please follow other related articles on the PHP Chinese website!