I'm having a small logic issue with joins. I have a database for answering answer. The schema is:
Question
question_id
question_text
Answers
answer_id
question_id
answer_text
User Responses
user_id
answer_id
question_id
I am trying to find questions that a user has not already answered but I keep getting null responses. The query is below:
SELECT * FROM questions
LEFT JOIN responses ON questions.question_id = responses.question_id
WHERE user_id != '1'
Where did my logic go wrong?
Try a LEFT JOIN with IS NULL
SELECT q.question_id FROM questions q
LEFT JOIN responses r ON q.question_id = r.question_id AND r.user_id = 1
WHERE r.question_id IS NULL
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With