找到没有孩子的一对多的父母

时间:2012-06-30 05:32:47

标签: sql one-to-many

说我下面有两个表,如何找到没有(0)答案的所有问题?

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

answer
------
id
question_id
content

question 1->* answers

- 编辑 -

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以使用以下查询找到。

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