I have two tables...
tblEvent
--------
id
date
tblEventRecurring
-----------------
event_id
date_part
end_date
The relationship is I can have an event that is either on one day (date in tblEvent) or recurs on an interval (Day, Week, Month, Year) which ties in the tblEventRecurring table.
For example, if I put in an event for Feb. 10 recurring until Feb. 28 I'd have...
tblEvent
--------
id = 1
date = 2012-02-10
tblEventRecurring
-----------------
event_id = 1
date_part = D
end_date = 2012-02-28
what is the best way structure a query to get all dates where the following conditions are met...
- tblEvent.date = today
- if tblEventRecurring.date_part = D and today < end_date
- if tblEventRecurring.date_part = W and today is the same day of week (sun-sat) as tblEvent.date and < end_date
- if tblEventRecurring.date_part = M and today is the same day of month as tblEvent.date and < end_date
etc.
Is there one solution more efficient than another? Is this a good time to use IF or CASE or a bunch of AND/OR statements?
Thanks for any guidance.
D.
Since I was down voted for not researching, I thought I'd post my final query... I was struggling with some of the AND/OR and parentheses. I DID research and was reading more on CASE which I've never used so I hoped someone could possibly guide my query using it.
# where the current date is 2012-02-12
SELECT e.record_id, e.event_date,
DATE_FORMAT(e.event_time, '%l:%i %p') AS event_time,
e.category, pm.person_id, pm.status, pm.active
FROM tblEvent e
JOIN tblTABLE pmd ON pmd.record_id = e.reference_id
JOIN tblTABLE2 pm ON pm.record_id = pmd.t_id
LEFT OUTER JOIN tblEventRecurring er ON er.event_id = e.record_id
WHERE e.category = 'CAT'
AND pm.planet_id = 1 # id of person
AND pm.active = 1
AND pm.status = 'Read Only'
AND (
e.event_date = '2012-02-12' # the event is on current date
OR (
# recurring with no end date or end date in future
er.end_date = '0000-00-00' OR er.end_date >= '2012-02-12'
AND (
e.event_date <= '2012-02-12' # recurring starts today or in past
AND ( # meets any of the following
(er.date_part = 'D')
OR (er.date_part = 'W' AND dayofweek('2012-02-12') = dayofweek(e.event_date))
OR (er.date_part = 'M' AND dayofmonth('2012-02-12') = dayofmonth(e.event_date))
OR (er.date_part = 'Y' AND (day('2012-02-12') = day(e.event_date) AND MONTH('2012-02-12') = MONTH(e.event_date)))
)
)
)
)
ORDER BY e.event_time
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…