SELECT a.*, b.numOfTakingExams
FROM employeeExam a
INNER JOIN
(
SELECT employee_id,
MAX(date) max_Date,
COUNT(*) numOfTakingExams
FROM employeeExam
GROUP BY course_ID, employee_id
) b ON a.employee_id = b.employee_id AND
a.date = b.max_Date
du kan også få den seneste post med det maksimale ID
hvis den er indstillet som AUTO_INCREMENT
, denne forespørgsel nedenfor giver det samme resultat som forespørgslen ovenfor,
SELECT a.*, b.numOfTakingExams
FROM employeeExam a
INNER JOIN
(
SELECT employee_id,
MAX(id) max_Date,
COUNT(*) numOfTakingExams
FROM employeeExam
GROUP BY course_ID, employee_id
) b ON a.employee_id = b.employee_id AND
a.id = b.max_Date