duandao3265 2011-01-31 17:44
浏览 54

我可以按另一个表中的记录数对SQL查询结果进行排序吗?

Something like this:

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

I'm using SQLite, but any examples should help.

  • 写回答

1条回答 默认 最新

  • du3932066 2011-01-31 17:51
    关注

    In MySQL, this would work:

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

    In SQLLite, you may need to add an extra layer like this:

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面