我正在尝试获取该问题的总答案数。(表结构见附图)
不知何故,我发现了这段代码,我相信它与我正在寻找的类似,但不确定如何显示总答案数(数据库中的类型 = A)
SELECT * FROM ^posts a, ^posts q WHERE a.parentid=q.postid AND a.type="A" AND q.type="Q"
如果你想要答案总数(类型 = 'A'),彼得给了你答案。
如果您想知道特定问题的答案数量,
SELECT COUNT(*) FROM posts a, posts q
WHERE a.parentid = q.postid
AND a.type='A' AND q.type='Q'
AND q.postid = <question_id_for_which_you_need_answer_count>
回到表中查看答案。LEFT JOIN 如果没有答案:
SELECT q.postid, count(a.*)
FROM posts p
LEFT OUTER JOIN posts a ON p.postid = a.parentid
WHERE p.type = "Q"
GROUP BY 1;
select q.postid, count(a.postid) from posts q
left outer join posts a on q.postid = a.parentid
where q.type = 'Q'
group by q.postid
你试过这个吗?
SELECT COUNT(*) FROM [table] WHERE type = 'A'
(为 [table] 插入您的表名)