开发者

Join 2 tables in mysql, return rows that don't relate

开发者 https://www.devze.com 2023-01-06 00:03 出处:网络
I have 2 tables: table 1: questions (id, question, date) table 2: answers (id, question_id, answer, date)

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 * FROMquestions,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 = "";

0

精彩评论

暂无评论...
验证码 换一张
取 消