Select all rows from a table where a value is NOT found? I have 3 tables:

ve_survey_questions

Qid | Question | CategoryId


ve_survey_answers

AnswerID | QuestionId | Answer


ve_survey_useranswers

id | UserId | QuestionId | AnswerId | Datetime


This is a multiple choice test. So ve_s_Q is all possible questions, ve_s_A is the possible multiple-choice answers, and ve_s_useranswers links the answer chosen by that particular user.

I'm trying to display every question that the user has NOT yet answered with a two step query:

    //check user answers
     $surveyanswerQ = "SELECT * FROM ve_survey_useranswers ORDER BY QuestionId";
     $surveyanswerResult = mysqli_query($db, $surveyanswerQ);
     $surveyanswer=  mysqli_fetch_assoc($surveyanswerResult);

     //select all unanswered questions
     $surveyQ = "SELECT * FROM ve_survey_questions q 
                          JOIN ve_survey_answers a 
                          ON q.Qid=a.QuestionId 
                          WHERE q.Qid &lt;&gt; {$surveyanswer['QuestionId']} 
                          ORDER BY a.QuestionId LIMIT 4 ";
     $surveyResult = mysqli_query($db, $surveyQ);


This does not work as supposed to... How can I write a better query?
So if I'm understanding the schema right, this should work with a LEFT OUTER JOIN with a subquery:

SELECT q.*, ua.UserId, ua.Answer
FROM ve_survey_questions q 
LEFT JOIN (
     SELECT u.*, a.QuestionId, a.Answer
     FROM ve_survey_useranswers u
     INNER JOIN ve_survey_answers a ON u.AnswerId = a.AnswerID
     WHERE u.UserId = @userid
     ) AS ua
ON q.Qid = ua.QuestionId 
WHERE ua.QuestionId IS NULL
ORDER BY q.Qid


So the subquery returns a row for each answered question for the given user (along with the answer they selected), then we use an outer join to see what questions are still unanswered.

You can eliminate the WHERE ua.QuestionId IS NULL if you instead want the full question list for that user, along with their answer, if there is one.