【问题标题】:Count one table and ORDER BY it计算一张桌子并按它排序
【发布时间】:2013-12-12 00:39:01
【问题描述】:

我想按 cmets 的数量对我的博客文章进行排序。要检索我的帖子等,我使用:

$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,我使用链接来“排序”博客。

要检索博客文章附带的 cmets 数量,我使用:

$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(*)'];

所以,我可以按日期、标题、主题、作者等排序。但是我如何设法按 cmets 的数量排序?

【问题讨论】:

标签: php sql sql-order-by


【解决方案1】:

这可能是遥不可及的。从我的头顶写在这里:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-11
    • 2021-03-14
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 2017-04-04
    • 2012-05-23
    相关资源
    最近更新 更多