【问题标题】:Get user with maximum count of posts Phalcon获取帖子数量最多的用户 Phalcon
【发布时间】:2017-04-21 01:24:33
【问题描述】:

我是 PHP 和 web 框架 Phalcon 的新手。我已经尝试了很多,但没有找到答案。我尝试使用它的 ORM,但不明白如何生成查询。

这是我在 SQL 中的查询:

SELECT username, count(*) maximum FROM user
    INNER JOIN post ON post.user_id = user.id
GROUP BY user.id
ORDER BY maximum DESC
LIMIT 15

请帮助使用 Phalcon ORM 生成查询。感谢您的任何回复:)

【问题讨论】:

    标签: php mysql database phalcon phalcon-orm


    【解决方案1】:

    根据@Juri 的回答,我已经这样做了:

    $result = User::query()
        ->columns('username, COUNT(post.id) as maximum')
        ->innerJoin('Post', 'post.user_id = User.id', 'post')
        ->groupBy('User.id')
        ->orderBy('maximum DESC')
        ->limit(15)
        ->execute();
    

    不知道对不对。但无论如何它对我有用。感谢帮助。 p.s.也许它也会对某人有所帮助:))

    【讨论】:

    • 是的,这是另一种解决方案。这可能会更好,因为您少了 2 种方法。使用任何你喜欢的东西。 Just 模型管理器具有更多扩展性并提供更多功能。
    【解决方案2】:
    $result = $modelsManager->createBuilder()
    ->columns('username,COUNT(post.id) as maximum')
    ->from(['user' => '<user class here>'])
    ->innerJoin('<post class here>', 'post.userId = user.id', 'post')
    ->groupBy('user.id')
    ->orderBy('maximum DESC')
    ->limit(15)
    ->getQuery()
    ->execute();
    

    【讨论】:

    • 为什么有人减去这个答案?这是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    • 2018-12-31
    • 2023-03-25
    • 2020-12-07
    • 1970-01-01
    • 2011-08-06
    相关资源
    最近更新 更多