select count(*) as 'number',left(createtime,7) as 'month' from t_hotel
where DATE_FORMAT(createtime,'%Y-%m')>
DATE_FORMAT(date_sub(curdate(), interval 12 month),'%Y-%m')
group by month;
說明:
我這裏查詢的是總量,可以根據需要將count(*)改爲合適字段。
t_hotel 表名
createtime 日期字段
number 別名
例如當前日期爲2019-01-19
使用上述sql語句查詢結果爲: