3

我有两个表(questions和),并且希望仅当相应的计数为零时才answers从中选择行,换句话说,当没有匹配的答案时。questionsanswersquestionid

到目前为止,我的查询是:

SELECT q.* , COUNT(a.id) FROM questions q LEFT OUTER JOIN answers a ON q.id = a.questionid WHERE COUNT(a.id)=0

我不确定是否可以这样做,但它不起作用。有任何想法吗?

谢谢

4

2 回答 2

4

尝试,

SELECT q.*
FROM questions q LEFT OUTER JOIN answers a 
           ON q.id = a.questionid 
WHERE a.questionid IS NULL
于 2012-11-19T14:49:33.437 回答
1

您可以使用not exsist如下:

SELECT q.*  
FROM questions q 
where not exists ( select 1 from answers  a
                   where q.id = a.questionid
                 )
于 2012-11-19T14:48:32.960 回答