3

我有一个表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值,但我不能只选择他最后一次参加考试的数据。有什么帮助吗?

4

5 回答 5

2
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

ID如果设置为,您还可以通过最大值获取最新记录,AUTO_INCREMENT下面的查询产生与上面查询相同的结果,

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
于 2013-01-15T12:57:01.310 回答
1

您是否尝试过加入自身?在第一个中,您选择包含“最后一次考试”的 ID,在第二个中,您加入您需要的东西。类似于以下内容:

select A.* FROM
employeexam A INNER JOIN (
    SELECT EMPLOYEE_ID, MAX(DATE)
    FROM EMPLOYEEXAM
    GROUP BY EMPLOYEE_ID
) B
ON A.EMPLOYEE_ID = B.EMPLOYEE_ID AND
A.DATE = B.DATE

当然,假设每个 Employee_id 的日期是唯一的。

于 2013-01-15T13:03:19.867 回答
1

试试这个查询 -

SELECT
  t1.id, t1.course_id, t1.employee_id, t1.degree, t1.date, t2.numOfTakingExams
FROM
  mployeexam t1
  JOIN (
        SELECT employee_id, MAX(date) date, COUNT(*) numOfTakingExams
        FROM mployeexam
        GROUP BY employee_id
    ) t2
    ON t1.employee_id = t2.employee_id AND t1.date = t2.date
于 2013-01-15T12:58:03.130 回答
0
SELECT x.*
     , y.ttl
  FROM employeexam x 
  JOIN 
     ( SELECT course_id
            , employee_id
            , MAX(date) max_date
            , COUNT(*) ttl 
         FROM employeexam 
        GROUP 
           BY course_id
            ,employee_id
     ) y 
    ON y.course_id = x.course_id 
   AND y.employee_id = x.employee_id 
   AND y.max_date = x.date;
于 2013-01-15T13:09:42.900 回答
0
SELECT  ee.*, num_exams
FROM    (
        SELECT  employee_id, COUNT(*) AS num_exams
        FROM    employeexam
        GROUP BY
                employee_id
        ) eed
JOIN    employeeexam ee
ON      ee.id =
        (
        SELECT  id
        FROM    employeeexam eei
        WHERE   eei.employee_id = eed.employee_id
        ORDER BY
                eei.employee_id DESC, eei.date DESC, eei.id DESC
        LIMIT 1
        )

这将正确处理在一个日期进行多次考试的情况。

于 2013-01-15T13:01:47.640 回答