簡體   English   中英

為每個條目選擇最新答案MySQL

[英]select the most recent answer for each entry MySQL

我覺得這真的很容易做到,我只是在某個地方犯了一些小錯誤。 可能應該補充一點,我是一名老師,而不是編碼員,所以我不太熟悉SQL。 另外,我確實在這里看了一堆問題,但都沒有解決。

我有表格student_answers(id, student_id, question_id, answer, result, date_time)我想獲取每個學生輸入的最后一個答案的question_idanswerresultdate_time 因此,如果他們對問題7回答了3次,我只想查看他們輸入的最后一個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被省略,因為每個問題都有一個稍后的答案

檢查以下查詢:(為所有學生提供所有問題的最新答案)

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM