I have a table called employeexam which structure and data are like this:
--------------------------------------------------------
| id | course_id | employee_id | degree | date
--------------------------------------------------------
| 1 | 1 | 3 | 8 | 2013-01-14
| 2 | 2 | 4 | 15 | 2013-01-14
| 3 | 2 | 4 | 17 | 2013-01-15
--------------------------------------------------------
Desired result would be:
---------------------------------------------------------------------------
| id | course_id | employee_id | degree | date | numOfTakingExams
---------------------------------------------------------------------------
| 1 | 1 | 3 | 8 | 2013-01-14 | 1
| 3 | 2 | 4 | 17 | 2013-01-15 | 2
---------------------------------------------------------------------------
My MySQL query:
SELECT DISTINCT(employeexam.employee_id) as employeeid,
employeexam.*,
exam.numOfTakingExams
FROM employeexam
JOIN (
SELECT employee_id , COUNT(employee_id ) as numOfTakingExams
FROM employeexam
GROUP BY employee_id
) exam
ON exam.employee_id = employeexam.employee_id
ORDER BY employeexam.id DESC
This outputs numOfTakingExams value correctly, but i can’t select only the data of the last time he entered an exam. Any help?
you can also get the latest record by the maximum
IDif it is set asAUTO_INCREMENT, this query below yields the same result from the query above,