简体   繁体   中英

MySQL time selection between two date stamps

I have a quandary. I have a database with two columns both of which are time stamps, these represent the start and end time of an event.

I also have two times which represent a start and end period for which I need to use to query the database to find out the rows whose event overlaps the start and end period.

This should work if the times given are either 06:00 to 23:00 as an example, but also when the reverse is true, 23:00 to 06:00 (ie 11pm one day to 6am the next).

Looking at the events,

Ev1 Start '2015-06-05 10:08:57' End '2015-06-05 16:18:27'

Ev2 Start '2015-06-05 16:08:57' End '2015-06-05 23:18:27'

Ev3 Start '2015-06-10 19:30:47' End '2015-06-11 01:43:17'

Having a time from and time to of 12:00 and 17:00 should return Ev1 and Ev2. A time of 17:00 to 12:00 should return Ev2 and Ev3.

I've got the search working when someone is purely looking for any events that occurred either after a certain time or before a certain time, it is when two times are given and then taking into account the times going across more than the single day.

I've started a SQLFiddle with the schema prebuilt : http://sqlfiddle.com/#!9/13616/1/0

Any help will be much appreciated!

Regards

[Event.start] <= [Timeframe.start] AND [Event.end] >= [Timeframe.start]
OR
[Event.start] BETWEEN [Timeframe.start] AND [Timeframe.end]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM