I have a bit of code that tells you when a contact would not have been contacted and highlights that day on a calender. i.e. If you last contacted the person yesterday, then a highlighted day will appear one month from that on next months page.
What I would like to do is do that for every month up to a year. So in the previous example, if I moved one month beyond the highlighted day, it would be ‘not contacted for two months’, three months, four months and so fourth up to 12 months.
Here is what I’m using now for the ‘not contacted for one month’ query:
SELECT DATE_FORMAT(DATE(DATE_ADD(date, INTERVAL 1 MONTH)), '%Y-%c-%d') AS overDate
FROM contact_method_history
WHERE DATE(DATE_ADD(date, INTERVAL 1 MONTH)) = '$SQLDate'
AND entityRef = ".$this->entityId."
ORDER BY date DESC
LIMIT 1
$this->entityId could be something like 153 or 9045, its just a reference to the contact.
$SQLDate is in the form of ‘2012-05-09’.
I don’t agree with comments: if you do a loop then you will run 12 queries instead of 1, which is decreasing performances. In the other hand, the statement for a “all in one” query is not trivial: