简体   繁体   中英

select the most recent answer for each entry MySQL

I feel like this should be really easy to do an I am just making some small mistake somewhere. Probably should add that I am a teacher and not a coder, so I'm not well versed in SQL. In addition, I did look at a bunch of questions here and none of them quite worked.

I have the table student_answers(id, student_id, question_id, answer, result, date_time) I want to get the question_id , answer , result and date_time for the last answer a student entered for each question. So if they answered three times for question 7, I only want to see the last answer and result that they entered.

For teaching purposes I can not simply update each row as they re-enter answers.

I tried the following queries

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

but that didn't include the questions with multiple answers at all, and only have me the questions that student 505 answered once. Student 505 answered questions 3 and 4 twice and the rest only once, and I only saw results for 1, 2 and 5.

I tried this query

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

but this on only gave me 3 and 4 and none of the ones he attempted only once. Any help would be greatly appreciated!

This is a sample of the data:

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

when I run the query I would like to see:

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 

Notice entries 7135 and 7137 are omitted since there is a later answer for each of those questions

Check this query: (gives all the students with latest answers to all questions)

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;

and for a specific student_id and specific question_id s:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM