Getting events from database that spans multiple days
I'm trying to get all events of a calendar stored in a database table
which are inside a specific range given by the user. It works until the
event spans multiple days. At that point the end date will not be in the
time range and so the query will discard it. My question is how to get all
the events that fall inside the given range, no matter if they start
before the rand and ends inside it or if they start inside the range and
end after.
CREATE TABLE IF NOT EXISTS `events` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
`start` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`end` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
AUTO_INCREMENT=1;
The current query
SELECT t0_.name AS name0, t0_.start AS start3, t0_.end AS end4
FROM events t0_
WHERE t0_.start > start
AND t0_.end < end
Example data:
name start end
-------------------- -------------------- --------------------
test event 2012-02-17 09:00:00 2012-02-17 09:50:00
test event2 2012-02-17 09:00:00 2012-02-19 09:00:00
test event3 2012-02-12 09:00:00 2012-02-17 09:00:00
test event4 2012-02-12 09:00:00 2012-02-13 09:00:00
Expected result data:
name start end
-------------------- -------------------- --------------------
test event 2012-02-17 09:00:00 2012-02-17 09:50:00
test event2 2012-02-17 09:00:00 2012-02-19 09:00:00
test event3 2012-02-12 09:00:00 2012-02-17 09:00:00
With range:
2012-02-17 09:00:00 - 2012-02-18 09:00:00
as you can see I take the events that are inside the given range, plus the
events that starts in the range and end after and the events that start
before the range but ends inside it.
No comments:
Post a Comment