gpt4 book ai didi

php - MySQL 查询对 GROUP BY 3 表计数中的结果进行排序

转载 作者:行者123 更新时间:2023-11-29 14:48:36 25 4
gpt4 key购买 nike

我正在编写一个列表系统,并尝试根据 2 个表中的评论和投票数对帖子进行排序。

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

最佳答案

尝试:

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

关于php - MySQL 查询对 GROUP BY 3 表计数中的结果进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6291060/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com