我有一个表employeexam
,它的结构和数据是这样的:
--------------------------------------------------------
| 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
--------------------------------------------------------
期望的结果是:
---------------------------------------------------------------------------
| id | course_id | employee_id | degree | date | numOfTakingExams
---------------------------------------------------------------------------
| 1 | 1 | 3 | 8 | 2013-01-14 | 1
| 3 | 2 | 4 | 17 | 2013-01-15 | 2
---------------------------------------------------------------------------
我的 MySQL 查询:
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
这可以正确输出numOfTakingExams
值,但我不能只选择他最后一次参加考试的数据。有什么帮助吗?