Hi ! I have a very simple question for a beginner in SQL. I have defined 3 tables in my database : "DOCTOR", "PATIENTS", "VISITS". For simplicity purposes, I have only a one-to-many relationship btw these tables : One doctor has many patients, but a patient can see only one doctor and one patient can make many visits... in my table "VISITS" I have a field "timestamp" to know when was the last visit.
I want to make a "view" on MySQL where I will have for each doctor the latest patient that came for a visit, and only the most recent one. Let's say each doctor sees 10 patient a day, I just want for each doctor the last guy that paid a visit to him.
How can I do such a view ?
Thanks for your help
EDIT 1
=> THANKs FOR your answers !
I finally opted for a more realistic approach where a "patient" can see many "doctor" Therefore, my table "VISITS" will look like this :
idVisit idDoctor idPatient timestamp 1 1 1 2010-07-19 14:10 2 1 2 2010-07-19 15:10 3 2 1 2010-07-19 15:10 4 3 1 2010-07-19 16:10 5 2 2 2010-07-19 18:10 6 2 3 2010-07-19 19:10 7 1 1 2010-07-19 20:10
I have then 3 patients and 3 doctors. For example, the patient 1 went two times to see doctor 1, one time doctor 2 and one time doctor 3.
I would like to build my request so that for each (doctor, patient) i have the last visit. This query should return the id Visits (2,3,4,5,6) and not 1 because the last visit that the patient paid to doctor 1 was at 20:10 and not 14:10. How could I do that ?
One more time, I really appreciate you comments and your help. It really helps me to improve my designs as I begin using SQL.