mysql date auxiliary table

MySQL table creation date

# 1 , num create a table for storing the numbers 0 ~ . 9 

the CREATE  TABLE num (I int ); 
# 2 , 0 in the generated table num ~ . 9 

the INSERT  the INTO num (I) the VALUES ( 0 ), ( 1 ), ( 2 ), ( 3 ), ( 4 ), ( 5 ), ( 6 ), ( 7 ), ( 8 ), ( 9 ); 
# 3 , generates a storage date table, DataList field names 

the CREATE  tABLE   IF  Not  EXISTS  calendar (datelist date);
# 4 , the data generated and inserted date

The INSERT  the INTO Calendar (datelist) the SELECT 
      ADDDATE ( 
          (    - where the starting date, the current date you can replace 
              the DATE_FORMAT ( " 2016 - . 1 - . 1 ", ' %% Y-M-% D ' ) 
          ), 
          numlist.id 
     ) the AS `date` in the previous
 the FROM 
( 
  the SELECT 
        n1.i + n10.i *  10  + n100.i *  100  + n1000.i *  1000 +n10000.i *  10000  the AS ID
         the FROM 
        NUM N1 
        the CROSS  the JOIN NUM the AS N10
         the CROSS  the JOIN NUM the AS N100
         the CROSS  the JOIN NUM the AS N1000
         the CROSS  the JOIN NUM the AS n10000 
     ) the AS numlist; 
# . 5 , and finally add the primary key to 

the ALTER  TABLE `calendar`
 the ADD  the COLUMN id` `   int UNSIGNED the NOT  NULL AUTO_INCREMENT the COMMENT ' master key ' FIRST,
 the ADD PRIMARY KEY (`id`);

 

Guess you like

Origin www.cnblogs.com/alittlesmile/p/11583809.html