[英]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.
可能应该补充一点,我是一名老师,而不是编码员,所以我不太熟悉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. 我有表格
student_answers(id, student_id, question_id, answer, result, date_time)
我想获取每个学生输入的最后一个答案的question_id
, answer
, result
和date_time
。 So if they answered three times for question 7, I only want to see the last answer
and result
that they entered. 因此,如果他们对问题7回答了3次,我只想查看他们输入的最后一个
answer
和result
。
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. 但这根本不包括多个答案的问题,只有我505学生回答一次的问题。 Student 505 answered questions 3 and 4 twice and the rest only once, and I only saw results for 1, 2 and 5.
学生505回答了问题3和4两次,其余的仅回答了一次,我只看到了1、2和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. 但这只给了我3和4,而他只给了我一次。 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 通知条目7135和7137被省略,因为每个问题都有一个稍后的答案
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: 对于特定的
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.