简体   繁体   中英

Having trouble with joining tables in query

DB Table Structure:

Session Table (aka Exam Table)

SessionId(auto)  SessionName
137              XULWQ

Question Table:

SessionId  QuestionId QuestionContent  QuestionNo QuestionMarks  OptionId
137        1          Name 2 Things     1         5               5
137        2          Name 3 Things     2         5               2

Option_Table Table:

OptionId  OptionType
1         A-C
2         A-D
3         A-E
4         A-F
5         A-G
6         A-H

Answer Table:

 AnswerId(auto) SessionId  QuestionId  Answer
   200            137        1           B
   201            137        1           F
   202            137        2           D
   203            137        2           A
   204            137        2           C   

I am having trouble compiling the query below. I want to select the following fields undeneath in the query but the problem I am having is with my joins. If you look at the last join I am trying to retrieve OptionID from Option_Table Table but I have to go through the Answer table which doesn't have that field. My problem is that I have 3 fields which actually rely on joining with the Question` Table.

My question is what is the correct way of joining the tables below so that it displays the answers for each question with the fields I want to SELECT?

SELECT q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, q.OptionId, o.OptionType
       FROM Session s 
       INNER JOIN Question q ON s.SessionId = q.SessionId
       JOIN Answer an ON q.QuestionId = an.QuestionId
       JOIN Option_Table o ON an.OptionId = o.OptionId
       WHERE s.SessionName = ?
       ORDER BY q.QuestionId, an.Answer

Output required:

在此处输入图片说明

Just change that JOIN condition to use the column from the table Question :

SELECT  q.SessionId, s.SessionName, q.QuestionId, q.QuestionNo, 
        q.QuestionContent, an.Answer, an.AnswerId, q.QuestionMarks, 
        q.OptionId, o.OptionType
FROM `Session` s 
INNER JOIN `Question` q 
    ON s.SessionId = q.SessionId
INNER JOIN `Answer` an 
    ON q.QuestionId = an.QuestionId
INNER JOIN `Option` o 
    ON q.OptionId = o.OptionId
WHERE s.SessionName = ?
ORDER BY q.QuestionId, an.Answer
SELECT * 
FROM Question q 
    INNER JOIN Answer a ON q.QuestionID = a.QuestionID
    INNER JOIN Option_Table ot ON ot.optionID = q.optionID
    INNER JOIN session s ON s.sessionid = q.sessionid
SELECT
    s.SessionId ,
    s.SessionName,
    q.QuestionId,
    q.QuestionNo,
    q.QuestionContent,
    a.Answer,
    a.AnswerId,
    q.QuestionMarks,
    ot.OptionId,
    ot.OptionType  
FROM    Session as s
LEFT JOIN Question as q ON s.SessionId = q.SessionId
LEFT JOIN Option_Table as ot ON ot.OptionId = q.OptionId 
LEFT JOIN Answer as a ON a.QuestionId = q.QuestionId    

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