I have 2 tables:
table 1: questions (id, question, date)
table 2: answers (id, question_id, answer, date)
a question can have more than 1 answer开发者_运维问答, some questions don't have answers. I want to output only unanswered questions
a query like SELECT * FROM
questions,
answersWHERE questions.id!=answers.question_id group by questions.id
doesn't work
Can someone help a newbie like me, I'm so lost in all this mysql stuff.
SELECT Q.id, Q.question, Q.date
FROM questions Q LEFT JOIN answers A ON (Q.id = A.question_id)
WHERE A.id IS NULL
SELECT id, question, date
FROM questions q
WHERE NOT EXISTS
(SELECT * FROM answers a
WHERE a.question_id = q.id)
OR
SELECT id, question, date
FROM questions q
LEFT JOIN answers a ON a.question_id = q.id
WHERE a.id IS NULL
OR
SELECT id, question, date
FROM questions q
WHERE q.id NOT IN
(SELECT question_id FROM answers WHERE question_id IS NOT NULL)
/* If question_id can't be NULL this is not needed*/
SELECT * FROM questions,answers WHERE answers.answer = null or answers.answer = "";
精彩评论