tags:

views:

84

answers:

4

I have a table of events with a recorded start and end time. I want to find all events that occur on a specific date. Some events started a year ago and some will continue farther ahead.

I would like to be able to pick for example May 20, 2010 and find all events occurring on that date.

+1  A: 
SELECT * FROM yourTable 
  WHERE start_date >= '2010-01-01' AND end_date <= '2010-06-30'

This query will return all dates where start_date is on or after 1st Jan, 2010, and end_date is on or before 30th June, 2010.

Click Upvote
That will give me all events between those dates, however, some of those events will have ended say between February and May of 2010, and I wouldn't want expired events to show.
Margaret
A: 

Assuming you have input parameters named @start_date and @end_date, and you want to find any event that overlaps that range:

select *
  from TableName
 where @start_date between start_date and end_date
    or @end_date between start_date and end_date
brianary
My code on the search page is as follows:$SearchResult=mysql_query("SELECT * FROM events WHERE start_date >= '$start_date' and end_date <= '$end_date'") or die(mysql_error());And on the calendar page I have:$cal->addEvent('', 2010, 3, 27, 'search/eventSearch.php?start_date=2010-03-27I have a feeling I am doing something completely wrong. I hope someone can help me. Please.
Margaret
OK, given two inputs, see changes above. You might be able to use $start_date rather than @start_date, depending on your php.ini.
brianary
This problem has been solved. I had an error on the search page irrelevant to what I have posted. Thank you for your help.
Margaret
+1  A: 
select * from TableName
where '2010-05-20' >= start_date and '2010-05-20' <= end_date

This will give you all events that occur on a specific date, even if the start or send dates are on different days.

James
This doesn't produce any records.
Margaret
A: 

Try...

WHERE (((MONTH(start_date) >= MONTH(NOW())) && (MONTH(end_date) <= MONTH(NOW()))) && YEAR(start_date) >= YEAR(NOW()))

That would get you anything from 3/2010.

Or..

WHERE ((MONTH(start_date) >= MONTH(NOW())) && YEAR(start_date) >= YEAR(NOW())))

Would get you events after 3/2010 into the future, and nothing before that.

Or..

WHERE (start_date_var) BETWEEN start_date AND end_date

Simply, where (passed value) is between any event start and end date.

These should produce results. It depends on how you are executing the query. Are you accepting form/url arguments to change dates?

It's confusing though that you want to have all dates that are not expired, yet also want dates that occurred on a specific date. Where are you getting start_date from?

Kevin
This is a link to a page that has a calendar at the bottom of it that is similar to mine: http://www.torontolife.com. I hope someone can post an idea on how to work this out.
Margaret