简体   繁体   中英

MySQL query date range - find common periods

Please accept my apologies for a lack of attempted code.

I can't get my head around this - or even figure out if it's feasible!

I have a table filled with 'date slots', these can be booked by an individual.

The time slots table looks like this:

ID | Price | Available_from | Available_ to    
------------------------------------
 1 | 20.00 | 2017-10-01 | 2018-01-01
 2 | 20.00 | 2017-11-01 | 2017-12-07
 3 | 20.00 | 2017-10-31 | 2018-01-31
 4 | 20.00 | 2017-10-22 | 2017-11-21    
------------------------------------

In these rows, there is a common date range where all four results are available, 2017-11-01 > 2017-11-21

I would like to query my database to see if 1) there is a common range for every result and 2) if there is, get the date range.

Desperate for help!

Thank you

I think this would work :

SELECT  MAX(Available_from) as MatchFrom, 
        MIN(Available_to) as MatchTo
FROM Temporary.test
WHERE 
  (SELECT MAX(Available_from) FROM Temporary.test)
  <=(SELECT MIN(Available_to) FROM Temporary.test)

It will return NULL, if there is no overlapping range.

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