假设我有下面两张表,我如何找到没有(0)个答案的所有问题?寻找没有孩子的一对多父母
question
---------
id
content
和
answer
------
id
question_id
content
即question 1->* answers
- 编辑 -
要添加到我的问题,我将如何检索每个问题的答案计数?
假设我有下面两张表,我如何找到没有(0)个答案的所有问题?寻找没有孩子的一对多父母
question
---------
id
content
和
answer
------
id
question_id
content
即question 1->* answers
- 编辑 -
要添加到我的问题,我将如何检索每个问题的答案计数?
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
你可以找到用低于查询。
Select * from question where id not in (Select question_id from answer)
嘿!我编辑了答案,你能检查增加的要求吗? – zsquare
@zsquare请提供更多详情。 –
你可以说'在(从答案中选择distinct(question_id)''减少子查询结果以匹配。 –
嘿!我编辑了答案,你能检查增加的要求吗? – zsquare