有兩個表格,一個是應答表,另一個是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";
我如何輸出爲陣列中的$行[ '...']?
你能發表你正在使用的查詢來獲取數據嗎?最有可能的是你需要內部加入答案表到你的查詢來獲得實際的答案。 – mellamokb
查詢已被添加到帖子 – BruceyBandit
供將來參考:它可以幫助你發佈一個精確的表模式。你缺少'Question' table'(QuestionId,QuestionNumber)',並且Answer中的列被稱爲'CorrectAnswer'而不是'AnswerCorrect'。也不知道'StudentId'和'SessionId'應該來自哪裏。 – mellamokb