stijn0713 Posted August 19, 2012 Share Posted August 19, 2012 $questions = mysql_query('SELECT SQL_CALC_FOUND_ROWS * FROM poll_questions WHERE ID_poll = "'.$_SESSION['poll_id'].'" ORDER BY position ASC LIMIT '.$start.', '.$limit.''); How can i prevent questions which are already answered by the same person from being retrieved in this query? Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/ Share on other sites More sharing options...
premiso Posted August 20, 2012 Share Posted August 20, 2012 You have provided too vague of an explanation. We do not know your table structures and thus, have no clue what would need to be filtered to do that. If you provide more information I am sure someone here can help you out. Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1370970 Share on other sites More sharing options...
fenway Posted August 20, 2012 Share Posted August 20, 2012 We can't infer your table structure from *. Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1370989 Share on other sites More sharing options...
stijn0713 Posted August 21, 2012 Author Share Posted August 21, 2012 Apologizes, I have 4 tables concerning polls: polls, poll_questions, poll_options, poll_answers. in survey.php i want to show each survey, with the belonging questions and belonging options to the questions. the tables: table: Poll_questions ID ID_poll poll_question question_type positon table: poll_answers ID ID_option ID_question ID_poll visitor_ip respondent_id option_label_case_schaal table: poll_options ID ID_question ID_poll poll_option schaallabel_links schaallabel_rechts position table: polls ID title I came with a sql instruction like: AND NOT EXISTS(SELECT poll_answers.ID FROM poll_answers WHERE poll_answers.ID_question = poll_questions.ID AND poll_answers.visitor_ip = '".mysql_real_escape_string(getIP())."') OR poll_answers.respondent_id = '".$_SESSION['resp_id].'"). it seems to work, in that questions which are already answered by that particular user, don't show anymore, but it seems that questions are also skipped, say start with question 2, then jump to 4, to 6, to 8. I don't have this problem with leaving out the AND NOT EXISTS SQL. then the questions show up nicely from question 1 to the last question. So i think there is still something wrong with the sql instruction? Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1371034 Share on other sites More sharing options...
jazzman1 Posted August 21, 2012 Share Posted August 21, 2012 I think, the sql logic in that case could be this : $query = "SELECT `q`.`poll_question` FROM `poll_questions` as `q` WHERE `q`.`ID_poll` NOT IN (SELECT `a`.`poll_answers.ID` FROM `poll_answers` AS `a` WHERE `a`.`respondent_id` = 2)"; Tip: The integer 2 comming from $_SESSION['resp_id']. Give back the result, please. Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1371285 Share on other sites More sharing options...
fenway Posted August 21, 2012 Share Posted August 21, 2012 NOT IN is evil -- use LEFT JOIN... IS NULL. Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1371297 Share on other sites More sharing options...
jazzman1 Posted August 21, 2012 Share Posted August 21, 2012 NOT IN is evil -- use LEFT JOIN... IS NULL. Yes, it should be much better. I belong to old generation Quote Link to comment https://forums.phpfreaks.com/topic/267317-help-with-query-that-prevents/#findComment-1371298 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.