Akanksha Akanksha - 5 months ago 9
MySQL Question

order by date starting from current month in MYSQL

I have a date type column in mysql table. I need to order the date starting from the current month independent of year.

tblEvent

event_date
__________
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15


As the current month is Jun therefore, the required Output should be:

event_date
__________
2018-06-21
2016-06-23
2016-07-14
2018-08-16
2016-10-20
1990-12-09
2016-12-15
2016-01-07
2016-01-13
2016-05-16
2016-05-22
2016-05-30



  1. Order must start from the current month
    month

  2. for example if the current month is Nov then the order must be
    Nov, Dec, Jan, Feb, Mar, ....., Oct

  3. also the respective dates must be in asc order.

  4. order must be independent of year(i.e. no matter what year is there
    in the date)


Answer
SELECT event_date
FROM tblEvent
ORDER BY
 IF(MONTH(event_date) < MONTH(NOW()), MONTH(event_date) + 12, MONTH(event_date)),
 DAY(event_date)

Info: