简体   繁体   English

MySQL 查询将结果按 GROUP BY 3 表计数排序

[英]MySQL query order the results in GROUP BY 3 Tables Count

I'm coding a listing system and I'm trying to get the posts ORDER by number of comments and votes FROM 2 tables.我正在编写一个列表系统,我正在尝试通过来自 2 个表的评论和投票数来获取帖子 ORDER。

Table1 : Lists => id, title, detail

Table2 : Votes => voteid, listid

Table3 : Comments => commentid, listid

WHERE MY Current query is WHERE MY 当前查询是

$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

it is showing me results perfectly but ORDER BY is Lists.id Instead of number of votes and comments它完美地向我展示了结果,但 ORDER BY 是 Lists.id 而不是投票数和评论数

Try:尝试:

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  

That is because you have ORDER BY Comments.listid in your SQL statement.那是因为您的 SQL 语句中有ORDER BY Comments.listid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM