1、问题
如题目所说,通过mysql获取某年某月所有的天数。如获取2014年2月的所有日期。
2、处理过程
2.1 创建一个数字辅助表
CREATE TABLE `nums` ( `key` int(11) NOT NULL, PRIMARY KEY (`key`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='数字辅助表';
2.2 创建一个存储过程为数字辅助表增加
DELIMITER $$ CREATE DEFINER=`root`@`%` PROCEDURE `create_nums`(cnt int unsigned) BEGIN declare s int unsigned default 1; truncate table nums; insert into nums select s; while s*2<=cnt do begin insert into nums select `key`+s from nums; set s=s*2; end; end while; END$$ DELIMITER ;
执行存储过程,增加1-50000进入数字辅助表
call create_nums(50000);
2.3 通过数字辅助表获取2014年2月1日到2014年2月31日
select CONCAT('2014-02-',lpad(n.key,2,'0') ) day from nums n where n.key < 32
2.3 在2.2中得到的
select * from ( select CONCAT('2014-02-',lpad(n.key,2,'0') ) day from nums n where n.key < 32 ) d where d.day <= last_day(DATE_FORMAT('2014-02-01','%Y-%m-%d')) ;