我想按评论数量订购我的博文。要检索我的帖子等我使用:
$result = DB::query("SELECT R.rumorID, R.rumor_author, R.rumor_title, R.rumor_text, T.topic_name, A.first_name, A.surname, T.topicID, R.datum_geplaatst
FROM Rumor R JOIN Account A
ON A.userID = R.rumor_author
JOIN Topic T
ON T.topicID = R.topicID
ORDER BY $sortby $order");
foreach ($result as $rumor) {
$r_id = $rumor['rumorID'];
$ sortby和$ order是$ _GET,我使用链接到“按订购”博客。
要检索博客文章附带的评论数量,我使用:
$commentcount = DB::query("SELECT count(*)
FROM Rumor R JOIN Comment C
ON R.rumorID = C.rumorID
WHERE R.rumorID = :rumor", array(':rumor' => $r_id));
$rumortext = $rumor['rumor_text'];
$date = $rumor['datum_geplaatst'];
$countcomments = $commentcount[0]['count(*)'];
所以,我可以按日期,标题,主题,作者等订购。但我如何按照评论数量订购?
答案 0 :(得分:0)
这可能很遥远。从头顶写作:
SELECT R.rumorID, R.rumor_author, R.rumor_title, R.rumor_text, T.topic_name, A.first_name, A.surname, T.topicID, R.datum_geplaatst, count(c.rumorID) AS commentCount
FROM Rumor R
JOIN Account A ON A.userID = R.rumor_author
JOIN Topic T ON T.topicID = R.topicID
LEFT JOIN Comment C ON R.rumorID = C.rumorID
GROUP BY R.rumorID
ORDER BY commentCount ASC