按日期顺序从MYSQL的当前月份开始。

时间:2021-11-13 22:44:11

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

我在mysql表中有一个日期类型列。我需要从现在这个月独立出来的日期起订日期。

tblEvent

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. 订单必须从当月开始
  3. for example if the current month is Nov then the order must be Nov, Dec, Jan, Feb, Mar, ....., Oct
  4. 例如,如果当月是11月,那么订单必须是11月、12月、1月、2月、3月……10月
  5. also the respective dates must be in asc order.
  6. 另外,相关日期必须按照asc顺序。
  7. order must be independent of year(i.e. no matter what year is there in the date)
  8. 顺序必须与年份无关。不管日期是哪一年

3 个解决方案

#1


3  

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

Info:

信息:

#2


1  

If I understand your question correctly, a soultion can be like this:

如果我正确地理解了你的问题,灵魂可以是这样的:

select
  event_date
from
  tblevent
order by
  (12+month(event_date)-month(current_date)) mod 12,
  day(event_date)

this query will sort all rows starting from the current month, ordered by month and day not considering the year. Please see a working fiddle here.

该查询将从当前月份开始排序所有行,按月和日排序,不考虑年份。请在这里看到一个工作小提琴。

#3


0  

I just try below query, Getting all records, starts from current month in desc order. like:- month 6, 5, 4, 3, 2, 1

我只是尝试下面的查询,获取所有的记录,从本月的desc订单开始。比如:-第6个月,第5个月,第4个月,第3个月,第2个月,第1个月

SELECT *
FROM `schedules`
WHERE date_format( `ScheduleDate` , "%m" ) <= date_format( NOW( ) , "%m" )
ORDER BY `ScheduleDate` DESC
LIMIT 0 , 30;

You can try in this way, Hope this will help.

你可以尝试一下,希望这能有所帮助。

#1


3  

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

Info:

信息:

#2


1  

If I understand your question correctly, a soultion can be like this:

如果我正确地理解了你的问题,灵魂可以是这样的:

select
  event_date
from
  tblevent
order by
  (12+month(event_date)-month(current_date)) mod 12,
  day(event_date)

this query will sort all rows starting from the current month, ordered by month and day not considering the year. Please see a working fiddle here.

该查询将从当前月份开始排序所有行,按月和日排序,不考虑年份。请在这里看到一个工作小提琴。

#3


0  

I just try below query, Getting all records, starts from current month in desc order. like:- month 6, 5, 4, 3, 2, 1

我只是尝试下面的查询,获取所有的记录,从本月的desc订单开始。比如:-第6个月,第5个月,第4个月,第3个月,第2个月,第1个月

SELECT *
FROM `schedules`
WHERE date_format( `ScheduleDate` , "%m" ) <= date_format( NOW( ) , "%m" )
ORDER BY `ScheduleDate` DESC
LIMIT 0 , 30;

You can try in this way, Hope this will help.

你可以尝试一下,希望这能有所帮助。