Hello Everyone,
I have been searching around for an answer to this question with no
luck. I hope that some of you may have a couple of good ideas I could
try.
I am running MySQL 4.1.20.
In one of my databases, I have two tables: patients, visits.
Patients:
| id | name | dob |
Visits:
| id | visitId | visitDate |
Each patient has several records in the visits table. The two are
linked by the "id" unique identifier.
I am trying to retrieve the visitId of each patient's most recent
visit. Here is what I have been trying so far.
SELECT patients.id, patients.name, patients.dob, visits.visitId,
MAX(visit.visitDate)
FROM patients
LEFT JOIN visits ON patients.id = visits.id
GROUP BY patients.id
The problem is that with this statement, the "visitId" that is
returned, does not correspond to the highest visit date. So the query
pulls the highest visit date as a result of the MAX function, but the
visitId it returns does not belong to the record of this same visit.
I have a feeling that I must be missing something simple/obvious.
Any ideas?
Thanks for all of the help in advance.
Andy