0

I know there's something wrong here. Can anyone point it out? I am trying to display question_body from questions and answers_body from answers.

EDIT : I got this error: Column 'question_id' in field list is ambiguous

 <?php
        $auctionSurvey = "SELECT question_id, survey_id, question_body, answer_body FROM questions
                          INNER JOIN answers ON answers.question_id = questions.question_id
                          WHERE survey_id='1'";
        $aucResult = mysql_query($auctionSurvey) or die (mysql_error());

        while($auctionRow = mysql_fetch_assoc($aucResult)){
            echo $auctionRow['question_body']. $auctionRow['answer_body'];
        }
?>
4
  • What's the problem? No data returned? An error message? Does the query work when you run it in the database? Commented Dec 18, 2013 at 19:09
  • @andrewsi I got this error: Column 'question_id' in field list is ambiguous. Commented Dec 18, 2013 at 19:12
  • That means that there's more than one column with that name in your SELECT fields. You just need to qualify it by telling the database which one you want to use: SELECT questions.question_id.... Commented Dec 18, 2013 at 19:13
  • you need to identify the table for question_id so questions.question_id Commented Dec 18, 2013 at 19:13

3 Answers 3

1

It looks like one problem is ambiguous column...

In the SELECT list, I don't think MySQL can figure out which table is referenced by question_id.

The normative pattern whenever two (or more) tables (row sources) are referenced is to QUALIFY every column reference...

SELECT q.question_id
     , q.survey_id
     , q.question_body
     , a.answer_body
  FROM questions q
  JOIN answers a 
    ON a.question_id = q.question_id
 WHERE q.survey_id='1'

There's two big benefits to this pattern (qualifying column references):

Firstly, it helps future readers "know" which columns are from which table, without having to look at the table definitions and figure out which table has which column name.

Secondly, it helps avoid "breaking" a SQL statement when a new column with the same name as a column in another table is added... having the column references qualified avoids the potential for breaking a SQL statement (causing a working SQL statement to start raising an ambiguous column exception.

Sign up to request clarification or add additional context in comments.

Comments

0

Your error indicates that 'question_id' is in both tables. Use an 'alias' to differentiate between the tables, like this:

SELECT Q.question_id, survey_id, question_body, answer_body 
FROM questions Q
  INNER JOIN answers A ON A.question_id = Q.question_id
WHERE survey_id='1'";

Comments

0

You have to differentiate between the two tables:

SELECT Q.question_id, 
       A.question_id, 
       Q.survey_id, 
       Q.question_body,
       A.answer_body 
FROM questions Q 
INNER JOIN answers A ON A.question_id = Q.question_id 
WHERE Q.survey_id='1'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.