This table is used to store sessions (events):
CREATE TABLE session ( id int(11) NOT NULL AUTO_INCREMENT , start_date date , end_date date ); INSERT INTO session (start_date, end_date) VALUES ("2010-01-01", "2010-01-10") , ("2010-01-20", "2010-01-30") , ("2010-02-01", "2010-02-15") ;
We do not want to have a conflict between the ranges.
Let's say we need to insert a new session from 2010-01-05 to 2010-01-25 .
We would like to know the conflicting sessions.
Here is my request:
SELECT * FROM session WHERE "2010-01-05" BETWEEN start_date AND end_date OR "2010-01-25" BETWEEN start_date AND end_date OR "2010-01-05" >= start_date AND "2010-01-25" <= end_date ;
Here is the result:
+----+------------+------------+ | id | start_date | end_date | +----+------------+------------+ | 1 | 2010-01-01 | 2010-01-10 | | 2 | 2010-01-20 | 2010-01-30 | +----+------------+------------+
Is there a better way to get this?
fiddle
mysql range
Pierre de LESPINAY Mar 30 '10 at 14:24 2010-03-30 14:24
source share