3

我正在使用 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,我的结果应该是problem_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 个答案。所有的问题都必须满足这个条件。

任何问题?

4

3 回答 3

3
select problem_id
from
(
    select q.problem_id, q.id, count(a.id) answercount
    from question q
    left join answer a on a.question_id = q.id
    group by q.problem_id, q.id
) g
group by problem_id
having min(answercount) >= 2

替代方案(例如 4 个答案)

select distinct q.problem_id
from question q
left join answer a on a.question_id = q.id
left join answer b on b.question_id = q.id and a.id < b.id
left join answer c on c.question_id = q.id and b.id < c.id
left join answer d on d.question_id = q.id and c.id < d.id
where d.id is not null

您可以根据需要扩展此模式。如果你真的需要一个参数化的查询,你可以做一些疯狂的事情,比如加入 6 次,改变 WHERE 子句如下:

where case when f.id is not null then 6
           when e.id is not null then 5
           when d.id is not null then 4
           when c.id is not null then 3
           when b.id is not null then 2
           else 1 end >= {{YourParamHere}}
于 2012-10-25T00:44:42.093 回答
2

这是我在 T-SQL 中的问题:

declare @problem table(id bigint not null primary key clustered)
declare @question table(id bigint not null primary key clustered, problem_id bigint)
declare @answer table(id bigint not null primary key clustered, question_id bigint)

declare @n int = 2

insert @problem
      select 1 
union select 2

insert @question
      select 1, 1 
union select 2, 1
union select 3, 1
union select 4, 2

insert @answer 
      select 1, 1 
union select 2, 1
union select 3, 1
union select 4, 2
union select 5, 2
union select 6, 3
union select 7, 4
union select 8, 4

select p.id --, p.name, p.description, p.etc
from @problem p
where @n >= ALL --http://msdn.microsoft.com/en-us/library/ms178543.aspx
(
    select COUNT(a.id) 
    from @question q
    left outer join @answer a
        on q.id = a.question_id
    where p.id = q.problem_id
    group by q.id
)

注意:表架构与问题略有不同,因为问题中的架构与示例数据不匹配。

选择

(基于@RichardTheKiwi 的回答,内部 SQL 已移至临时表)

declare @tempTable table (pid bigint, qid bigint, aidCount bigint)

insert @tempTable
select q.problem_id, q.id, count(a.id) answercount
from @question q
left join @answer a on a.question_id = q.id
group by q.problem_id, q.id

select pid
from @tempTable
group by pid 
having min(aidCount) >= @n 
于 2012-10-25T00:46:07.207 回答
1

重写为相关子查询。我们不是在每个问题中找到至少有 N 个答案的问题,而是找到没有少于 N 个答案的问题的问题:

SELECT id AS problem_id
FROM problem AS p 
WHERE NOT EXISTS
      ( SELECT 1
        FROM question AS q
          LEFT JOIN answer AS a 
            ON a.question_id = q.id
        WHERE q.problem_id = p.id
        GROUP BY q.id
        HAVING COUNT(a.question_id) < 2
      ) ;
于 2012-10-25T22:06:29.583 回答