Jump to content

sql statement help


woodplease

Recommended Posts

I'm creating a quiz script so that a user can enter a quiz name, and then add questions and answers to that quiz.

 

I have an SQL statement where I join 2 tables (questions and answers), and which is part of a query that echoes out criteria that matches the SQL. I need to change the SQL statement so that it also only prints out the data if it matches data in a column from the table holding the quiz name, i.e it only prints out the questions and answers for that particular quiz.

 

This is what I thought it would be, but it doesn't work. It keeps printing out the answers from all of the quizes.

 

$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref FROM questions JOIN answers ON questions.questionno = answers.questionno WHERE questions.questionno = ".$row['questionno']. "AND answers.quizref = ".$id;

 

Any help would be great

 

Thanks

Link to comment
Share on other sites

i've tried adding another join statement, but its not working. any ideas?

$query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions 
JOIN answers ON questions.questionno = answers.questionno 
WHERE questions.questionno = ".$row['questionno']. "JOIN answers ON quiz.quizref = answers.quizref WHERE quiz.quizref = ".$id;

Link to comment
Share on other sites

You have to do the joins prior to the where, you have them mixed in.

 

I am not sure if my syntax is right but give this a try:

 

   $query2 = "SELECT questions.questionno, questions.question, questions.quizref, questions.questionref, answers.answerno, answers.answer, answers.answervalue, answers.questionno, answers.quizref, answers.answerref, quiz.quizref, quiz.name, quiz.createdby FROM questions
   JOIN answers ON questions.questionno = answers.questionno JOIN quiz ON quiz.quizref = answers.quizref
   WHERE questions.questionno = ".$row['questionno']. " AND quiz.quizref = ".$id;

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.