having an issue with my MySQL query that I can not figure out...
I'm trying to get a count of how many rows are in the table "questions" that are not in the table "answers" where the user_id matches
This is my MySQL at the moment and it returns 0 rows even though it should return 13...
SELECT COUNT(*) AS total
FROM questions
LEFT OUTER JOIN answers
ON questions.quest_id = answers.ans_question
JOIN users
ON answers.ans_user = users.user_id
WHERE answers.ans_question IS null
and questions.quest_level <= (SELECT user_level
FROM users
WHERE username = 'chris3spice')
and answers.ans_user = (SELECT user_id
FROM users
WHERE username = 'chris3spice');
This is my original query which returns how many are in questions that aren't in answers... but it doesn't take into account user_id but it does take into account the user_level so no issues there...
SELECT COUNT(*) AS total
FROM questions
LEFT OUTER JOIN answers
ON questions.quest_id = answers.ans_question
LEFT OUTER JOIN users
ON answers.ans_user = users.user_id
WHERE answers.ans_question IS null
and questions.quest_level <= (SELECT user_level
FROM users
WHERE username = 'chris3spice');
Here are my tables for reference
quest_id-----quest_text------quest_ans
1__________blah________blah
...
14_________blah________blah
.
.
ans_id-----ans_user------ans_quest
1__________1________1
...
14_________2________13
.
. user_id-----user_name
1_________chris3spice
2_________testing
select count(*) from questions left join answers on questions.quest_id = answers.ans_question where answers.ans_question is nulland see if you get what you expect for that part.answers.ans_user = .... When you are LEFT JOINing and looking for unmatched records, answers.ans_user will be NULL and thus false.