Here's my query:
SELECT cases.case_id, cases.county_id, case_name, county.county_name, date_referral,
date_flag, auto_flag, sessions.date_entry, curdate() - date_flag AS days_overdue
FROM cases
INNER JOIN county ON cases.county_id = county.county_id
LEFT JOIN sessions ON cases.case_id = sessions.case_id
WHERE date_flag <= curdate( )
AND sessions.date_entry IS NULL
AND date_flag IS NOT NULL
AND auto_flag = "0"
AND status_id = "0"
ORDER BY cases.county_id, days_overdue DESC , case_id ASC
LIMIT 0 , 30
Here's an example result set:
13-1206-B McC v Jac 2013-08-15 91
13-0951-B Gen v Gen 2013-08-16 90
13-1160-B Bla v Bur 2013-08-20 86
12-3048-B Wor v Wor 2013-08-29 77
13-0903-B Ben v Ben 2013-09-01 5
13-0500-B Bru v Bru 2013-09-05 1
13-1238-B Bal v Bal 2013-09-05 1
13-0135-B TDA v Tan 2013-09-06 0
The columns are case_id (irrelevant), case_name (irrelevant), date_flag, and the result of the equation "curdate() - date_flag" as days_overdue.
As you can see, the results are pretty inconsistent. Today is 2013-09-06 and it has not been 91 days since 2013-08-15. It has not been 77 days since 2013-08-29. But it has been 5 days since 2013-09-01, and it has been 0 days since 2013-09-06. So the latter half of the results are correct but the former...I don't even know where it's getting those numbers from.
Any ideas what's going wrong with this query?
Thanks!