comparing dates by month and year in mysql

前端 未结 4 1957
醉话见心
醉话见心 2021-02-13 18:11

I have a table containing data about events and festivals with following columns recording their start and end dates.

  • Start_Date
  • End_Date

d

4条回答
  •  暖寄归人
    2021-02-13 18:40

    Comparing the year and month separately feels messy. I like to contain it in one line. I doubt it will make a noticeable difference in performance, so its purely personal preference.

    select * from your_table
    where LAST_DAY(Start_Date) = LAST_DAY(curdate())
    and end_date <= curdate() + interval 30 day
    

    So all I'm doing is using the last_day function to check the last day of the month of each date and then comparing this common denominator. You could also use

    where DATE_FORMAT(Start_Date ,'%Y-%m-01') = DATE_FORMAT(curdate(),'%Y-%m-01')
    

提交回复
热议问题