为每个条目MySQL选择最新的答案

时间:2013-03-13 07:00:36

标签: mysql group-by greatest-n-per-group

我觉得这应该很容易做到我只是在某个地方犯了一些小错误。可能应该补充说我是老师而不是编码员,所以我不太熟悉SQL。另外,我确实在这里看了一堆问题,但没有一个问题很有用。

我有表student_answers(id, student_id, question_id, answer, result, date_time)我想得到question_idanswerresultdate_time,以获得学生为每个问题输入的最后一个答案。因此,如果他们针对问题7回答了三次,我只想查看他们输入的最后answerresult

出于教学目的,我不能简单地更新每一行,因为他们重新输入答案。

我尝试了以下查询

SELECT id, question_id, answer, result, date  FROM Student_Answers
WHERE student_id = 505 AND question_id in (select id from Test_Questions q where q.test_id = 37) 
Group by question_id
having date = max(date)
ORDER BY Student_Answers`.`question_id` ASC

但这根本不包括多个答案的问题,而且只有我学生505回答一次的问题。学生505回答问题3和4两次,其余只回答一次,我只看到1,2和5的结果。

我试过了这个查询

SELECT
    b.*
FROM
    (
        SELECT question_id, MAX(date) AS maxdate
        FROM TP_Student_Answers
        GROUP BY question_id
    ) a
INNER JOIN
    TP_Student_Answers b ON 
        a.question_id = b.question_id AND 
        a.maxdate = b.date 
        and b.student_id = 505 
        and b.question_id in (select id from TP_Questions q where q.test_id = 37)
ORDER BY
    b.question_id

但这只给了我3和4,而且没有他只尝试过一次。任何帮助将不胜感激!

这是数据样本:

id   student_id question_id answer  result      date 

7133     505    1      a    correct 2012-11-16 09:03:58

7134    505 2      c    wrong   2012-11-16 09:03:58

7135    505 3      e    wrong   2012-11-16 09:03:58

7136    505 3      d    wrong   2013-12-16 09:03:58

7137    505 4      c    correct 2012-11-16 09:03:58

7138    505 4      d    wrong   2013-12-16 09:03:58

7139    505 5       blank   2012-11-16 09:03:58

当我运行查询时,我希望看到:

7133      505   1      a    correct 2012-11-16 09:03:58

7134    505 2      c    wrong   2012-11-16 09:03:58

7136    505 3      d    wrong   2013-12-16 09:03:58

7138    505 4      d    wrong   2013-12-16 09:03:58

7139    505 5       blank   2012-11-16 09:03:58 

通知条目7135和7137被省略,因为每个问题都有后来的答案

1 个答案:

答案 0 :(得分:0)

检查此查询:(为所有学生提供所有问题的最新答案)

SELECT id, student_id, question_id, answer, result, date_time
  FROM (SELECT *,
               CASE
                  WHEN (@prevQ = question_id AND @prevS = student_id)
                  THEN
                     @marker := 0
                  ELSE
                     @marker := 1
               END
                  AS marker,
               @prevS := student_id,
               @prevQ := question_id
                  FROM student_answers
                ORDER BY student_id ASC, question_id ASC, date_time DESC) aView,
               (SELECT @prevQ = -1, @prevS = -1) a
 WHERE marker = 1;

以及特定的student_id和特定的question_id

SELECT id, student_id, question_id, answer, result, date_time
  FROM (SELECT *,
               CASE
                  WHEN (@prevQ = question_id AND @prevS = student_id)
                  THEN
                     @marker := 0
                  ELSE
                     @marker := 1
               END
                  AS marker,
               @prevS := student_id,
               @prevQ := question_id
          FROM student_answers
         WHERE     student_id = 501
               AND question_id IN (SELECT id
                                     FROM TP_Questions q
                                    WHERE q.test_id = 37)
        ORDER BY student_id ASC, question_id ASC, date_time DESC) aView,
       (SELECT @prevQ = -1, @prevS = -1) a
 WHERE marker = 1;