Link to home
Start Free TrialLog in
Avatar of m175400
m175400

asked on

Selecting records according to date

Hi there,

I'm having trouble selecting events from my database appropriate to a specific month and year of the user's choice.

Suppose for the following examples, the user wishes to view all records where the event is during, or overlaps into, April 2007.

The following query produces results:

SELECT tblevents.*, DATE_FORMAT(tblevents.event_expires,'%d.%m.%Y') as event_expires, DATE_FORMAT(tblevents.event_to,'%d.%m.%Y') as event_to, DATE_FORMAT(tblevents.event_from,'%d.%m.%Y') as event_from FROM tblevents WHERE (tblevents.event_from BETWEEN '2007/04/01' AND '2007/04/30') OR (tblevents.event_to BETWEEN '2007/04/01' AND '2007/04/30') ORDER BY event_from ASC

However, this version does not:

SELECT tblevents.*, DATE_FORMAT(tblevents.event_expires,'%d.%m.%Y') as event_expires, DATE_FORMAT(tblevents.event_to,'%d.%m.%Y') as event_to, DATE_FORMAT(tblevents.event_from,'%d.%m.%Y') as event_from FROM tblevents WHERE ('2007/04/01' BETWEEN tblevents.event_from AND tblevents.event_to) OR ('2007/04/30' BETWEEN tblevents.event_from AND tblevents.event_to) ORDER BY event_from ASC


The first version works largely, but if an event spans from say January - June, it won't return such rows because the to and from date column values fall outside the current month. I figured I could fix this using the 2nd query shown about, but that doesn't seem to want to return any rows at all. I guess the BETWEEN syntax doesn't take date columns as the arguments in that way? Why, I can't understand though.

Can someone please tell me how to fix this? I know I could adjust my system to record an event_id against a date in a new table, but I doubt there's any need for that and some nice SQL should yield the results I require...can it?

Hope you can help!

Many thanks.
Avatar of Louis01
Louis01
Flag of South Africa image

This one below should work unless your event can start before April and end after April.
If so, you need to do a cross join to a calendar containing an entry for every day.

SELECT tblevents.*, DATE_FORMAT(tblevents.event_expires,'%d.%m.%Y') as event_expires,
DATE_FORMAT(tblevents.event_to,'%d.%m.%Y') as event_to,
DATE_FORMAT(tblevents.event_from,'%d.%m.%Y') as event_from
FROM tblevents
WHERE (year(tblevents.event_from) = 2007 or year(tblevents.event_to) = 2007)
AND (month(tblevents.event_from) = 4 or year(tblevents.event_to) = 4)
ORDER BY event_from ASC


Cross join to a calendar containing an entry for every day - Example

SELECT tblevents.*, DATE_FORMAT(tblevents.event_expires,'%d.%m.%Y') as event_expires,
DATE_FORMAT(tblevents.event_to,'%d.%m.%Y') as event_to,
DATE_FORMAT(tblevents.event_from,'%d.%m.%Y') as event_from
FROM tblevents cross join (select caldate from mycalendar where mycalendar.caldate BETWEEN '2007/04/01' AND '2007/04/30') mycalendar
WHERE mycalendar.caldate between tblevents.event_from and tblevents.event_to
Avatar of m175400
m175400

ASKER

Sorry, so how does mycalendar table work?

Trying to visualise this. Do I just need one column with every date day by day for like 30yrs over which the site could be used? Or do I need an entry for each day the event is on e.g.

tblcalendar
calendar_id
calendar_date
event_id

?
ASKER CERTIFIED SOLUTION
Avatar of Louis01
Louis01
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin