2
事情是這樣的:我可以按另一個表中的記錄數對SQL查詢結果進行排序嗎?
SELECT count(Answers.ID) as answertotal, Questions.*
FROM Questions
LEFT JOIN Answers ON Answers.qid=Questions.ID
ORDER BY answertotal
我使用SQLite,但任何例子應該有所幫助。
事情是這樣的:我可以按另一個表中的記錄數對SQL查詢結果進行排序嗎?
SELECT count(Answers.ID) as answertotal, Questions.*
FROM Questions
LEFT JOIN Answers ON Answers.qid=Questions.ID
ORDER BY answertotal
我使用SQLite,但任何例子應該有所幫助。
在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
什麼是失敗在你目前的查詢? – 2011-01-31 17:47:31