MySQL obtains all the date points of a month.

Source: Internet
Author: User

MySQL obtains all the date points of a month.
1. Problems

As the title says, use MySQL to get all the days of a month in a certain year. For example, obtain all dates of January 1, February 2014.

2. handling process

2.1 create a digital auxiliary table

Create table 'nums' ('key' int (11) not null, PRIMARY key ('key') ENGINE = InnoDB default charset = utf8 COMMENT = 'Number auxiliary table ';

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

DELIMITER $$CREATE DEFINER=`root`@`%` PROCEDURE `create_nums`(cnt int unsigned)BEGINdeclare 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 ;

Execute the stored procedure and Add 1-to the digital auxiliary table

call create_nums(50000);

2.3 obtain data from January 1, February 1-20, 2014 to January 1, February 31 through the digital auxiliary table

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

The data obtained in 2.3 is less than the date equal to the last day of 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')) ;

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.