2011-01-31 40 views

回答

4

在MySQL中,這會工作:

SELECT count(Answers.ID) as answertotal, Questions.* 
FROM Questions 
LEFT JOIN Answers ON Answers.qid=Questions.ID 
GROUP BY Questions.ID 
ORDER BY answertotal 

在SQLLite,您可能需要添加額外的一層是這樣的:

SELECT q.*, tots.answertotal 
FROM Questions q 
INNER JOIN ( 
    SELECT count(Answers.ID) as answertotal, Questions.ID as questionid 
    FROM Questions 
    LEFT JOIN Answers ON Answers.qid=Questions.ID 
    GROUP BY Questions.ID 
) tots ON tots.questionid = q.ID 
ORDER BY tots.answertotal 
相關問題