Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:35:02+00:00 2026-05-26T08:35:02+00:00

There are two tables, one is an Answer table and the other is a

  • 0

There are two tables, one is an Answer table and the other is a StudentAnswer table. There are 6 fields I am interested in, 4 in Answer table and 2 from StudentAnswer table. Below are tables and their fields and data.

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 field contain AnswerId’s, these are student answers depending on which answer they selected for which question)

Now these fields and other fields are stored in an array which are displayed in a table. PHP code for this is below:

<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>";
        }
      ?>

Below shows what it outputs at moment from the query and array:

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

Row 1 shows what student u0867587 has put for his answer for question 1. The correct answer is leeds and as he selected answerid ‘1’ which is Leeds, his student answer is Leeds but obviously as StudentAnswer is an int field it displays it as ‘1’. Row 2 shows that student u1231231 answered the same question, obviously the correct answer is still leeds but he selected birmingham which is answerid ‘3’ so his answer is Birmingham but as StudentAnswer is int field it dislays it as ‘3’ The AnswerContent is linked to the AnswerId field so it whows word answers for AnswerId

Below is what I really want to output:

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

It outputs the same as the one above but instead of displaying it as an int from StudentAnswer field, it displays it as the word answer using AnswerContent. So AnswerContent is linked to Answerid automatically but I also want to link it with StudentAnswer.

StudentAnswer field is the same as AnswerId as it retrieves the AnswerId depending on what the student has chosen for his answer.

I did try $row['StudentAnswerContent'] == $row['StudentAnswer'] = $row['AnswerContent']; but it hasn’t worked. How can I display Answercontent for each StudentAnswer?

Below is query:

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";

How do I output it as a $row[‘…’] in the array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:35:02+00:00Added an answer on May 26, 2026 at 8:35 am

    Your query as posted looks fine, except you will need to specify the table alias for CorrectAnswer in your WHERE clause because it is ambiguous (could come from either Answer a or Answer a2:

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

    Working Demo: http://sqlize.com/Wk0LXJQ7De

    Also, since your column is called StudentAnswerContent that contains the student’s answer text, you will need to change that in your PHP:

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

    For future reference: It helps if you post an accurate table schema. You are missing Question (QuestionId, QuestionNumber) table, and the column in Answer is called CorrectAnswer not AnswerCorrect. Also have no idea where StudentId and SessionId are supposed to come from. Also, if you want help with a query, it kinda helps to have the query in the your post that you’re working with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

One doubt in MSSQL. There are two tables in a databases. Table 1 named
There are two tables. One is users info users, one is comments info comments.
I have two product tables Product1 and Product2. There is a one 2 one
I have two tables one has a three column composite key. The other needs
Code below from other stackoverflow answer is used in jqGrid to implement checkbox using
alt text http://img502.imageshack.us/img502/7245/75088152.jpg There are two tables that I join them together, one of
Consider these two classes mapped to the same table. One is readonly via mutable=false.
I need to merge two SELECT statements. There are two tables in my database,
I am developing a student registration form in asp.net. there are two tables viz
There is a store procedure that uses FREETEXTTABLE twice on two tables and then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.