2011-10-20 29 views
1

有兩個表格,一個是應答表,另一個是StudentAnswer表。有6個我感興趣的領域,4個應答表和2個StudentAnswer表。以下是表格及其字段和數據。將每個'StudentAnswer'字段顯示爲其每個關聯的'AnswerContent'字段(php)

Answer Table: 

    QuestionId  AnswerId AnswerContent AnswerCorrect 
    1    1   Leeds    1 (true) 
    2    2   Manchester   0 (false) 
    3    3   Birmingham   0 (false) 

StudentAnswer Table: 

    StudentAnswer QuestionId 
     2    1 
     3    1 
     1    1 

(StudentAnswer場包含AnswerId的,這些都是取決於哪個回答他們選擇哪個問題學生回答)

現在,這些領域和其他領域都存儲在其中顯示在表中的數組。此PHP代碼如下:

<table border='1'> 
     <tr> 
     <th>Session ID</th> 
     <th>Question Number</th> 
     <th>AnswerContent</th> 
     <th>StudentAnswer</th> 
     <th>Student ID</th> 
     </tr> 
     <?php 

     while ($row = mysql_fetch_array($result)) { 

      echo " 
     <tr> 
     <td>{$row['SessionId']}</td> 
     <td>{$row['QuestionNo']}</td> 
     <td>{$row['AnswerContent']}</td> 
     <td>{$row['StudentAnswer']} </td> 
     <td>{$row['StudentId']}</td> 
     </tr>"; 
     } 
     ?> 

下面顯示了它在瞬間輸出從查詢和數組:

Session ID Question Number Answerid AnswerContent  StudentAnswer Student ID  
ABB   1    1   Leeds     1   u0867587 
ABB   1    1   Leeds     3   u1231231 

行1只顯示了學生u0867587已經把他的回答對問題1正確的答案是利茲,並且當他選擇利茲的答案「1」時,他的學生答案是利茲,但很顯然,因爲StudentAnswer是一個int字段,所以它顯示爲「1」。第2行顯示,學生u1231231回答了同樣的問題,顯然正確的答案仍然是利茲,但他選擇了伯明翰,答案是'3',所以他的答案是伯明翰,但是因爲StudentAnswer是int字段,所以它將其解釋爲'3'AnswerContent已鏈接到AnswerId字段,以便其whows爲AnswerId

字的回答下面是我真正想要輸出:

Session ID Question Number AnswerId AnswerContent  StudentAnswer  Student ID  
    ABB   1    1  Leeds    Leeds   u0867587 
    ABB   1    1  Leeds    Birmingham  u1231231 

它輸出爲上述之一,但不是顯示它從StudentAnswer領域的int ,它使用AnswerContent將它顯示爲單詞答案。所以AnswerContent會自動關聯到Answerid,但我也想將它與StudentAnswer關聯。

StudentAnswer字段與AnswerId相同,因爲它根據學生爲答案選擇的內容來檢索AnswerId。我試過$row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent'];但它沒有奏效。我如何顯示每個StudentAnswer的Answercontent?

下面是查詢:

SELECT 
    SessionId, 
    q.QuestionNumber, 
    a.AnswerContent, 
    a2.AnswerContent as StudentAnswerContent, 
    StudentId 
FROM Question q 
    INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId 
    JOIN Answer a ON sa.QuestionId = a.QuestionId 
    JOIN Answer a2 ON sa.StudentAnswer = a2.AnswerId 
WHERE 
    (CorrectAnswer = '1') 
ORDER BY $orderfield ASC"; 

我如何輸出爲陣列中的$行[ '...']?

+0

你能發表你正在使用的查詢來獲取數據嗎?最有可能的是你需要內部加入答案表到你的查詢來獲得實際的答案。 – mellamokb

+0

查詢已被添加到帖子 – BruceyBandit

+0

供將來參考:它可以幫助你發佈一個精確的表模式。你缺少'Question' table'(QuestionId,QuestionNumber)',並且Answer中的列被稱爲'CorrectAnswer'而不是'AnswerCorrect'。也不知道'StudentId'和'SessionId'應該來自哪裏。 – mellamokb

回答

0

張貼您的查詢看起來不錯,但你需要在你的WHERE子句指定爲CorrectAnswer表的別名,因爲它是不明確的(可能來自任何Answer aAnswer a2

WHERE 
    (a.CorrectAnswer = '1') 
    ^^ specify alias 

工作演示:http://sqlize.com/Wk0LXJQ7De

此外,由於你的列稱爲StudentAnswerContent包含學生的答覆文件,則需要更改您的PHP

<td>{$row['StudentAnswerContent']} </td> 
         ^^^^^^^ add "Content" 

以供將來參考:它幫助,如果你發佈一個準確的表模式。您缺少Question (QuestionId, QuestionNumber)表,並且Answer中的列被稱爲CorrectAnswer而不是AnswerCorrect。也不知道StudentIdSessionId應該來自哪裏。另外,如果你想要查詢的幫助,它有助於在你的文章中查詢你正在使用的查詢。

+0

它已經非常感謝你,我被困在那裏2天,並不能解決這個問題:) – BruceyBandit

+0

@ user992147:只是好奇,你是否看到一個錯誤信息,當你運行該查詢?如果你不是(也應該是),你可能想在你的開發環境中嘗試使用'error_reporting(E_ALL)'來幫助調試這樣的問題,或者在終結處添加'或die(mysql_error())'任何對「mysql_query」的調用。另外,你可以看看'var_dump'命令,它對調試你正在獲取的數據真的很有幫助。如果您在for循環中執行了'var_dump($ row)',則可能有助於診斷您未獲得預期結果的原因。 – mellamokb

+0

我正在使用模具(mysql_error()0,但它沒有帶有任何錯誤,它只是顯示錶單和查詢結果的空表,它已經工作,雖然如此謝謝你 – BruceyBandit

相關問題