2011-06-09 58 views
0

我正在编写一个列表系统,我试图通过评论数和投票数从2个表中获取帖子ORDER。MySQL的查询命令在GROUP BY 3中的结果表计数

Table1 : Lists => id, title, detail 

Table2 : Votes => voteid, listid 

Table3 : Comments => commentid, listid 

WHERE我现在查询是

$q = mysql_query("SELECT * FROM zoo_leads 
LEFT JOIN Votes ON Lists.id=Votes.listid 
LEFT JOIN Comments ON Lists.id=Comments.listid 
GROUP BY Lists.id ORDER BY Comments.listid DESC LIMIT 10 

它显示了我的结果完美,但ORDER BY是Lists.id相反投票数和评论

+0

也许问题是,有没有COUNT()。 – 2011-06-09 10:01:15

回答

1

尝试:

SELECT * 
FROM zoo_leads 
     LEFT JOIN votes 
     ON lists.id = votes.listid 
     LEFT JOIN comments 
     ON lists.id = comments.listid 
GROUP BY lists.id 
ORDER BY COUNT(votes.id) DESC, 
      COUNT(comments.id) DESC 
LIMIT 10 
0

那是因为你有ORDER BY Comments.listid你的SQL语句。

+0

我已重新删除Comments.listid但失败... – 2011-06-09 10:02:10