Php 我可以按另一个表中的记录数对SQL查询结果进行排序吗?
大概是这样的:Php 我可以按另一个表中的记录数对SQL查询结果进行排序吗?,php,sql,database,sqlite,Php,Sql,Database,Sqlite,大概是这样的: SELECT count(Answers.ID) as answertotal, Questions.* FROM Questions LEFT JOIN Answers ON Answers.qid=Questions.ID ORDER BY answertotal SELECT q.*, tots.answertotal FROM Questions q INNER JOIN ( SELECT count(Answers.ID) as answertotal,
SELECT count(Answers.ID) as answertotal, Questions.*
FROM Questions
LEFT JOIN Answers ON Answers.qid=Questions.ID
ORDER BY answertotal
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
我正在使用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 count(Answers.ID) as answertotal, Questions.*
FROM Questions
LEFT JOIN Answers ON Answers.qid=Questions.ID
ORDER BY answertotal
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
当前查询中失败的是什么?