带计数的简单sql查询
我正在使用SQLite。 我需要一个简单问题的帮助。 这是我的三张桌子:带计数的简单sql查询,sql,sqlite,count,nested,Sql,Sqlite,Count,Nested,我正在使用SQLite。 我需要一个简单问题的帮助。 这是我的三张桌子: -------------- problem -------------- id (primary key) question_id (foreign key) -------------- question -------------- id (primary key) answer_id (foreign key) -------------- answer -------------- id (primary k
--------------
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
但它不起作用,因为它至少有一个问题有两个答案。所有问题都必须满足该条件
有什么问题吗
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答案
以下是我对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
作为相关子查询重写。我们不是在每个问题中找到至少有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
) ;
这似乎是正确的,但我的sqlite版本有一些问题,所以在执行select from select时出现了一个错误。。。。是否有其他方法编写该查询?谢谢我没有忘记。在外部软件中,它可能使用不同的sqlite版本,但在我的软件中,它不工作,我无法更改它,因此我必须以另一种方式编写该查询,可能使用INTERSECT?我为您添加了一个替代查询库。您能解释一下替代查询中的条件minanswercount>=2在哪里吗?结果与第一个不同。也许这就是我错过了不同结果的地方。它可以左键连接两次以得到两个不同的答案a.id
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
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
) ;