Mysql 查询近12个月
select date_format(curdate(),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 1 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 2 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 3 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 4 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 5 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 6 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 7 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 8 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 9 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 10 month),'%Y-%m') AS `mon` union
select date_format((curdate() - interval 11 month),'%Y-%m') AS `mon`
执行结果:
CURDATE() 函数返回当前的日期
UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据