Jay
Jay

Reputation: 443

In MySQL grab items within start_date and end_date

I have events listed in my MySQL database.

Events have a start_date (date) and end_date (date) fields.

In my application users can click on a specific day using a calendar. So, if they click today they are taken to /events?day=2013-08-08 and all events from that day are shown on the page.

This was working great until I started adding multi day events. Since previously I was just asking MySQL for all events where start_date == the user chose date, I only get events that START on that day, BUT I also want events that may have started before that day but are still going ON that day.

How can I query items like this? All events that either start on this day, or started earlier but havent yet ended by this day.

Upvotes: 0

Views: 211

Answers (3)

Travis Hegner
Travis Hegner

Reputation: 2495

If your end date is not yet set for the tasks still going on:

where '2013-08-08' == start_date or end_date is null

Upvotes: 0

Dave
Dave

Reputation: 3288

where start_date>='$inputdate' AND end_date<='$inputdate'

Should do it

Upvotes: 0

Marc B
Marc B

Reputation: 360572

SELECT ...
FROM yourtable
WHERE '2013-08-08' BETWEEN start_end AND end_date

where

foo BETWEEN bar AND baz

is the SQL equivalent of

(bar <= foo) AND (foo <= baz)

Upvotes: 8

Related Questions