Problem:
Find people whose birthdays are tomorrow (table a), who havent got a record with an issue date set in the past 360 days from (table b)
Table a ID, DOB Table b ID, PID, Issued
I’ve got a query but it’s pretty slow, not sure if a join would be quicker – any help appreciated..
SELECT a.ID, a.DOB FROM a
WHERE MONTH(a.DOB)=MONTH(now()) # match month
AND DAYOFMONTH(a.DOB)=DAYOFMONTH(now()+ INTERVAL 1 DAY) # match day of month
AND (
SELECT COUNT(*) FROM b
WHERE b.PID = a.ID
AND b.Issued < DATE_SUB(SYSDATE(), INTERVAL 360 DAY)
) < 1 # hacky subquery to find not issued in past 360 days
1 Answer