1、CURDATE():返回当前的日期。
2、查询当前年月
SELECT DATE_FORMAT(CURDATE(),'%Y-%m') AS `year_month`
3、当前日期添加 1 个月
INTERVAL 1 MONTH
4、查询近6个月的月份
SELECT DATE_FORMAT(CURDATE(),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 1 MONTH),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 2 MONTH),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 3 MONTH),'%Y-%m')AS` year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 4 MONTH),'%Y-%m')AS` year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 5 MONTH),'%Y-%m')AS` year_month`
5、查询近6个月发布的文章数
SELECT t1.`year_month`,count(t2.id) as total FROM
(
SELECT DATE_FORMAT(CURDATE(),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 1 MONTH),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 2 MONTH),'%Y-%m')AS `year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 3 MONTH),'%Y-%m')AS` year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 4 MONTH),'%Y-%m')AS` year_month`
UNION SELECT DATE_FORMAT((CURDATE()-INTERVAL 5 MONTH),'%Y-%m')AS` year_month`
)t1
LEFT JOIN mxg_article t2
ON t1.`year_month`= DATE_FORMAT(t2.create_date,'%Y-%m')
AND t2.`status`=2 AND t2.ispublic=1
GROUP BY t1.`year_month`;