MySQL查询命令结果在GROUP BY 3 Tables Count中

时间:2011-06-09 09:58:37

标签: php mysql

我正在编制一个列表系统,我正试图通过2个表格的评论和投票来获得ORDER的帖子。

Table1 : Lists => id, title, detail

Table2 : Votes => voteid, listid

Table3 : Comments => commentid, listid

我的当前查询在哪里

$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而不是投票数和评论数

2 个答案:

答案 0 :(得分: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  

答案 1 :(得分:0)

这是因为你的SQL语句中有ORDER BY Comments.listid