我不知道如何解释这个但我想要做的是将两个数据库字段合并在一起并使用php输出新字段。例如:
Answer Table:
AnswerId: 10
AnswerContent: Manchester
AnswerId :11
AnswerContent: Leeds
AnswerId:12
AnswerContent:Birmingham
StudentAnswer Table:
Student: Bob
StudentAnswer:10
Student: Jim
StudentAnswer:11
我想要做的是当我运行我将在下面用PHP编码显示的查询时,对于StudentAnswer字段,我希望它显示答案的名称而不是答案ID。如何实现这一目标?
以下是代码:
<?php
if (isset($_POST['submit'])) {
$query = "
SELECT * FROM Question q
INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
JOIN Answer a ON sa.QuestionId = a.QuestionId
WHERE
('".mysql_real_escape_string($sessionid)."' = '' OR q.SessionId = '".mysql_real_escape_string($sessionid)."')
AND
('".mysql_real_escape_string($questionno)."' = '' OR q.QuestionNo = '".mysql_real_escape_string($questionno)."')
AND
('".mysql_real_escape_string($studentid)."' = '' OR sa.StudentId = '".mysql_real_escape_string($studentid)."')
AND(CorrectAnswer = '1')
ORDER BY $orderfield ASC";
$num = mysql_num_rows($result = mysql_query($query));
mysql_close();
?>
<p>
Your Search:
<strong>Session ID:</strong> <?php echo (empty($sessionid)) ? "'All Sessions'" : "'$sessionid'"; ?>,
<strong>Question Number:</strong> <?php echo (empty($questionno)) ? "'All Questions'" : "'$questionno'"; ?>,
<strong>Student Username:</strong> <?php echo (empty($studentid)) ? "'All Students'" : "'$studentid'"; ?>,
<strong>Order Results By:</strong> '<?php echo $ordername; ?>'
</p>
<p>Number of Records Shown in Result of the Search: <strong><?php echo $num ?></strong></p>
<table border='1'>
<tr>
<th>Session ID</th>
<th>Question Number</th>
<th>Question</th>
<th>Correct Answer</th>
<th>StudentAnswer</th>
<th>Correct Answer Weight</th>
<th>Student Answer Weight</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['QuestionContent']}</td>
<td>{$row['AnswerContent']}</td>
<td>{$row['StudentAnswer']}</td>
<td>{$row['Weight%']}</td>
<td></td>
<td>{$row['StudentId']}</td>
</tr>";
}
?>
</table>
<?php
}
?>
谢谢
答案 0 :(得分:0)
我认为你可以使用:
SELECT q.*, a.AnswerContent FROM Question q
INNER JOIN StudentAnswer sa ON q.QuestionId = sa.QuestionId
INNER JOIN Answer a ON sa.QuestionId = a.QuestionId
WHERE ....
我的意思是,你应该返回答案的文本(你还在加入表格,所以你确实有这个字段)。