2011-10-18 12 views
0

これを説明する方法はわかりませんが、私がしたいのは、2つのデータベースフィールドをマージしてphpを使って新しいフィールドを出力するものです。例えば:私は何をしたいかフィールドをまとめてphpとsqlの出力を表示する方法

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

ありがとう:

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 .... 

私が意味する、あなたがテキストを返す必要があります答えのために(あなたはSTIですあなたはそのフィールドを持っています)。

+0

@ user992147:私の答えはあなたに助けてくれましたか?あなたはあなたの問題を解決しましたか? – Marco

関連する問題