【发布时间】:2012-10-25 00:26:43
【问题描述】:
我正在使用 SQLite。 我需要帮助来解决一个简单的问题。 这是我的三张桌子:
--------------
problem
--------------
id (primary key)
question_id (foreign key)
--------------
question
--------------
id (primary key)
answer_id (foreign key)
--------------
answer
--------------
id (primary key)
我想获得在每个问题中至少有 N 个答案的所有问题。我给你举个例子:
-------
problem
id
1
2
-------
question
id problem_id
1 1
2 1
3 1
4 2
-------
answer
id question_id
1 1
2 1
3 1
4 2
5 2
6 3
7 4
8 4
如果 n=2,我的结果应该是 issue_id=2。
我试过了:
select distinct question.problem_id
from answer, question
where answer.question_id = question.id
group by answer.question_id
having count(*) >= 2
但它不起作用,因为它会遇到至少一个问题至少有 2 个答案的问题。所有问题都必须满足该条件。
有什么问题吗?
【问题讨论】: