计算行数并仅获取表中的最后一行

时间:2013-01-15 12:50:21

标签: mysql sql select

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

5 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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

答案 2 :(得分: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的日期是唯一的。

答案 3 :(得分: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
        )

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

答案 4 :(得分: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;