woodplease Posted January 31, 2010 Share Posted January 31, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/ Share on other sites More sharing options...
Mchl Posted January 31, 2010 Share Posted January 31, 2010 What is the significance of questions.questionno? Id it unique, or can same question number appear in two or more quizes? Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004623 Share on other sites More sharing options...
woodplease Posted January 31, 2010 Author Share Posted January 31, 2010 questions.questionno is not unique, i have a different unique field in that table, questions.questionref Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004625 Share on other sites More sharing options...
Mchl Posted January 31, 2010 Share Posted January 31, 2010 You should use this one as a JOIN condition. Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004631 Share on other sites More sharing options...
woodplease Posted January 31, 2010 Author Share Posted January 31, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004641 Share on other sites More sharing options...
premiso Posted January 31, 2010 Share Posted January 31, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/190449-sql-statement-help/#findComment-1004642 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.