MySQL gets all the date points for a month

Source: Internet
Author: User

1. Questions

As the title says, get all the days of the month in a certain year through MySQL. such as getting all dates for February 2014.

2. Processing process

2.1 Creating a digital auxiliary table

CREATE TABLE `nums` (  `key` int(11) NOT NULL,  PRIMARY KEY (`key`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘数字辅助表‘;

2.2 Create a stored procedure to add data to a digital auxiliary table

DELIMITER $$CREATEdefiner= ' root ' @ '% ' PROCEDURE 'create_nums'(cnt int unsigned)BEGINdeclare s int unsigned default 1;TRUNCATE TABLE nums; Insert intoNumsSelectS whiles*2<=cnt Do        beginInsert intoNumsSelect' Key ' +s fromNumsSets=s*2;End;End  while;END$ $DELIMITER;

Execute stored procedure, increase 1-50000 to enter digital auxiliary table

call create_nums(50000);

2.3 Obtained from February 1, 2014 to February 31, 2014 through the Digital auxiliary table

select CONCAT(‘2014-02-‘,lpad(n.key,2,‘0‘) ) day  from nums n where n.key < 32

2.3 The date in 2.2 that is less than or equal to the last day of February 2014 is the result we want.

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‘)) ;

MySQL gets all the date points for a month

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.