MySQL DATETIME choose between dates

I have a mySQL table with names with 4 fields (event_id, event_name, event_start_date, event_end_date)

The problem is getting the right events

I would get a record for all active events over a period of time, for example, between 2011/03/01 to 2011/03/30

  • event 1 starts on 2011/03/10 and ends on 2011/03/20 (start and end inside)
  • event 2 starts on 2011/02/05 and ends on 2011/03/23 (starts before and ends inside)
  • event 3 starts in 2011/03/25 and ends in 2011/05/01 (starts from ends after)
  • event 4 starts on 2011/01/25 and ends on 2011/10/12 (starts before and ends after)

All events are active during the considered time period and must be received as records.

I do not know how to make it work correctly! Solutions? Suggestions? Ideas?

thanks

+4
source share
3 answers

Assuming @Start and @End keep the date range you are looking for:

SELECT * FROM Events -- Exclude items known to be outside the range WHERE NOT ((event_end_date < @Start) OR (event_start_date > @End)) 
+3
source

Try the following:

 SELECT event_id, event_name, event_start_date, event_end_date FROM events WHERE event_start_date <= '2011-03-30' AND event_end_date >= '2011-03-01' 
+2
source
 Select * From temp where mydate >= '2011-03-10' and mydate =< '2011-03-20'; 

etc.

0
source

Source: https://habr.com/ru/post/1383338/


All Articles