MySQL querying birthdays in two ways

Source: Internet
Author: User

Need is to query the date period of the birthday of the member, divided into two situations:

1. Do not cross-year

For example: query for birthdays between 2017-01-01 and 2017-01-20 (assuming today is 2017-01-01, this can also be described as a 20-day birthday of the member)

Select *  from User where Date_format (Birthday,'%m-%d'between'01-01'  and ' 01-20 '

2. Cross-year

For example: query for birthdays between 2017-12-27 and 2018-01-05 members, (assuming today is 2017-12-27 this can also be described as a birthday in 10 days)

Here is the starting time of this year and the last day of this year as 1 sets of conditions, 2017-12-27 to 2017-12-31; Remove the first day of the year to the end of the next year as 1 sets of conditions: 2018-01-01 to 2018-01-05

Select *  from User where(Date_format (Birthday,'%m-%d')between '12-27'  and '12-31' ORDate_format (Birthday,'%m-%d')between '01-01'  and '01-05')

Analysis of two kinds of query birthday situation, there is also a case of a leap year birthday, stay until next time.

We have a good plan to welcome the exchange.

MySQL querying birthdays in two ways

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.