mysql函数-计算两个月份之间的月份

阅读更多
CREATE  FUNCTION `months_between`(`big_date` date,`small_date` date) RETURNS int(11)
BEGIN
	 declare diffMonth int;
   declare diffYear int;
   declare diffDay int;
   declare number int;

  select month(big_date)-month(small_date) ,day(big_date)-day(small_date) ,year(big_date)-year(small_date) into diffMonth,diffDay,diffYear;


if diffMonth > 0 and diffDay >= -1 then
   set number=diffYear*12 + diffMonth;
elseif diffMonth > 0 and  diffDay < -1 then
		 set number=diffYear*12 + diffMonth-1;
elseif diffMonth < 0 and  diffDay > -1 then 
    set number=diffYear*12 - (month(small_date)-month(big_date))+1;
elseif diffMonth = 0 and diffDay >= -1 then
    set number=diffYear*12 ;
else 
    set number = diffYear*12 - (month(small_date)-month(big_date));
end if;

return number;

END

 

你可能感兴趣的:(mysql,函数,两个日期之间的月份)