We have two table where where table A and table B need to be join. Relationship between table A and B is one to many. We can join now using this LEFT JOIN tableB on tableA.aID=tableB.aID. We would like to ensure only one record is join on table B and that is the latest record only. We now get more then one record.
SELECT student.*
FROM student
LEFT JOIN (SELECT B1.*
FROM medicalRecord AS B1
LEFT JOIN medicalRecord AS B2
ON B1.studentID = B2.studentID
AND B1.checkUpDate< B2.checkUpDate
WHERE B2.studentID IS NULL) AS b
ON ( student.studentID = medicalRecord.studentID)
Where student.studentID=45
Let us assume you want to get only the record from the child table with the highest value in the
valcolumn. You could use:The idea is to use a subquery to filter the table for the records with the greatest values in the
valcolumn, then to join on that.