好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

MySQL 获取某月所有的日期点

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中得到的数据中小于等于2014年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‘ )) ;  
$(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘ ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i ‘).text(i)); }; $numbering.fadeIn(1700); }); });

MySQL 获取某月所有的日期点

标签:mysql   数字辅助表   一个月所有天数   统计   procedure   

查看更多关于MySQL 获取某月所有的日期点的详细内容...

  阅读:22次