0

我在下面有一个查询:

SELECT sa.StudentId, GROUP_CONCAT( DISTINCT StudentAnswer
ORDER BY StudentAnswer
SEPARATOR ',' ) AS StudentAnswer
FROM Student st
INNER JOIN Student_Answer sa ON ( st.StudentId = sa.StudentId )
INNER JOIN Student_Response sr ON ( sa.StudentId = sr.StudentId )
INNER JOIN Question q ON ( sr.QuestionId = q.QuestionId )
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE q.SessionId =27
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId

它输出的结果在这里:

在此处输入图像描述

现在我计划编写一个更大的查询,因此我将所有这些表都包含在FROM

但结果中的问题是StudentAnswer列。它显示学生的重复学生答案。这是不正确的,好像我通过从FROM语句中删除一些表来使查询稍微小一些,然后它显示正确的学生答案:

SELECT sa.StudentId, q.QuestionId, GROUP_CONCAT( DISTINCT StudentAnswer
ORDER BY StudentAnswer
SEPARATOR ',' ) AS StudentAnswer
FROM Student st
INNER JOIN Student_Answer sa ON ( st.StudentId = sa.StudentId )
INNER JOIN Question q ON ( sa.QuestionId = q.QuestionId )
WHERE q.SessionId =27
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId

在此处输入图像描述

我的问题是,我能否StudentAnswer在第一个查询的列下获得正确的学生答案?

以下是表格列表:

学生:

StudentId (PK auto)  StudentForename  StudentSurname 
39                   Luke             McFadzen
40                   Chris            Tucker 

学生回答:

StudentAnswerId (PK auto)  QuestionId (FK Question)  StudentAnswer  StudentId (FK student)
1                          72                         D             39
2                          73                         B             39
3                          73                         C             39
4                          73                         D             39
5                          72                         C             40
6                          73                         A             40
7                          73                         C             40
8                          73                         E             40

学生响应:

StudentResponseId (PK auto)  QuestionId (FK Question) ResponseTime  StudentId (FK student)
1                            72                       00:00:05      39
2                            73                       00:00:15      39
3                            72                       00:00:09      40
4                            73                       00:00:26      40

问题:

QuestionId (PK auto)  QuestionNo  SessionId (FK Session) ReplyId (FK Reply) OptionId (FK Option)    
72                    1           23                     1                  3
73                    2           23                     2                  7

回答:

AnswerId (PK auto)    QuestionId (FK Question)      Answer  
1                          72                         C             
2                          73                         A             
3                          73                         C             
4                          73                         D             

回复:

ReplyId (PK auto)  ReplyType
1                  Single
2                  Multiple

选项表:

ReplyId (PK auto)  ReplyType
1                  A-C
2                  A-D
3                  A-E
4                  A-F
5                  A-G
6                  A-H
7                  A-I
8                  A-J

完整查询:

SELECT
sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId, q.QuestionId,
QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, 
GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
FROM Student st
INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
INNER JOIN Question q ON (sr.QuestionId = q.QuestionId)
INNER JOIN Answer an ON q.QuestionId = an.QuestionId
LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE q.SessionId =27
GROUP BY sa.StudentId, q.QuestionId
ORDER BY StudentAlias, q.SessionId


Student - Student_Answer (StudentId)
Student - Student_Response (StudentId)
Student_Answer - Question (QuestionId)
Student_Response - Question (QuestionId)
Question - Answer (QuestionId)
Question - Reply (ReplyId)
Question - Option_Table (OptionId)
4

1 回答 1

0

实际上你的查询已经在工作了,你只需要添加这个条件

SELECT  sa.StudentId
    ,   StudentForename
    ,   StudentSurname
    ,   q.SessionId
    ,   q.QuestionId
    ,   QuestionNo
    ,   o.OptionType
    ,   GROUP_CONCAT(DISTINCT Answer ORDER BY Answer SEPARATOR ',') AS Answer
    ,   r.ReplyType
    ,   GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer
    ,   ResponseTime
FROM    Student st
        INNER JOIN Student_Answer sa
            ON (st.StudentId = sa.StudentId)
        INNER JOIN Student_Response sr
            ON (sa.StudentId = sr.StudentId)
                AND sa.QuestionID = sr.QuestionID -- <<==lacking condition
        INNER JOIN Question q
            ON (sr.QuestionId = q.QuestionId)
        INNER JOIN Answer an
            ON q.QuestionId = an.QuestionId
        LEFT JOIN Reply r
            ON q.ReplyId = r.ReplyId
        LEFT JOIN Option_Table o
            ON q.OptionId = o.OptionId
GROUP   BY sa.StudentId , q.QuestionId;

或者,您可以使用子查询连接值

SELECT  DISTINCT sa.StudentId
    ,   StudentForename
    ,   StudentSurname
    ,   q.SessionId
    ,   q.QuestionId
    ,   QuestionNo
    ,   o.OptionType
    ,   an.QuestionAnswer
    ,   r.ReplyType
    ,   sa.StudentAns
    ,   ResponseTime
FROM    Student st
        INNER JOIN 
        (
            SELECT  StudentID, QuestionID,
                    GROUP_CONCAT(StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') StudentAns
            FROM    Student_Answer
            GROUP   BY StudentID, QuestionID
        )sa ON (st.StudentId = sa.StudentId) 
        INNER JOIN Student_Response sr
            ON (sa.StudentId = sr.StudentId) AND sa.QuestionID = sr.QuestionID
        INNER JOIN Question q
            ON (sr.QuestionId = q.QuestionId) 
        INNER JOIN 
        (
            SELECT  QuestionID, 
                    GROUP_CONCAT(DISTINCT Answer ORDER BY Answer) QuestionAnswer
            FROM    Answer
            GROUP   BY QuestionID
        ) an
            ON q.QuestionId = an.QuestionId
        LEFT JOIN Reply r
            ON q.ReplyId = r.ReplyId
        LEFT JOIN Option_Table o
            ON q.OptionId = o.OptionId
于 2013-02-08T07:43:41.610 回答