2012-06-30 68 views
0

假设我有下面两张表,我如何找到没有(0)个答案的所有问题?寻找没有孩子的一对多父母

question 
--------- 
id 
content 

answer 
------ 
id 
question_id 
content 

question 1->* answers

- 编辑 -

要添加到我的问题,我将如何检索每个问题的答案计数?

回答

4
select * from question q where not exists (select 1 from answer a where a.question_id=q.id) 

回答编辑的问题:

select q.id, content, count(a.id) 
    from question q 
    left outer join answer a 
    on q.id = a.question_id 
group by q.id 
+0

嘿!我编辑了答案,你能检查增加的要求吗? – zsquare

0

你可以找到用低于查询。

Select * from question where id not in (Select question_id from answer) 
+0

嘿!我编辑了答案,你能检查增加的要求吗? – zsquare

+0

@zsquare请提供更多详情。 –

+0

你可以说'在(从答案中选择distinct(question_id)''减少子查询结果以匹配。 –