views:

22

answers:

3

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,answers WHERE 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 :/ thanks everyone for your time!

+1  A: 
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*/
Martin Smith
thank you. didn't expected it to be so quick. thank you again, it works. love mysql
olegb
+2  A: 
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
Artefacto
A: 

SELECT * FROM questions,answers WHERE answers.answer = null or answers.answer = "";

Mowgli