【问题标题】:Multi-Dimensional groupBy & orderBy before Paginate - Laravel 5分页之前的多维 groupBy 和 orderBy - Laravel 5
【发布时间】:2016-01-22 09:57:43
【问题描述】:

我想要做的是将用户编写的具有相同主题的 cmets 分组,并获得最新的(最近的)一个。我尝试使用Comment::where()->orderBy()->groupBy(),但它没有按预期返回数据。

这是我的数据库:

| id | receiver_id | sender_id | title | body | created_at |
| 13 |      2      |     5     |  Art  |  DD  |   12.30... |
| 12 |      2      |     5     |  Art  |  CC  |   12.20... |
| 11 |      2      |     5     |  Art  |  BB  |   12.10... |
| 10 |      2      |     5     |  Art  |  AA  |   12.00... |

| 9  |      2      |     3     |  Msc  |  XX  |   11.30... |
| 8  |      2      |     3     |  Msc  |  YY  |   11.20... |
| 7  |      2      |     3     |  Msc  |  ZZ  |   11.10... |

| 6  |      2      |     2     |  Foo  |  UU  |   10.40... |
| 5  |      2      |     2     |  Foo  |  II  |   10.30... |

| 4  |      2      |     2     |  You  |  QQ  |   10.20... |
| 3  |      2      |     2     |  You  |  WW  |   10.10... |

| 2  |      2      |     3     |  Msc  |  LL  |   10.00... |

| 1  |      2      |     4     |  CSS  |  VV  |   10.30... |
| 0  |      2      |     4     |  CSS  |  NN  |   10.20... |

我意识到,created_at 和 ids 的顺序相同。所以,我决定使用id desc,因为我可能无法在created_at 字符串中排列日期整数。这是我尝试过的:

$comment = Comment::where('receiver_id', Auth::user()->id)
                  ->orderBy('id','desc')
                  ->groupBy('sender_id', 'title')
                  ->paginate(5);
dd($comment);

我想要得到的结果是这样的:

0 => Comment { id = 13, sender_id = 5, title = Art, body = DD }(最新创建的_at)

1 => Comment { id = 9, sender_id = 3, title = Msc, body = XX }

2 => Comment { id = 6, sender_id = 2, title = Foo, body = UU }

3 => Comment { id = 4, sender_id = 2, title = You, body = QQ }

4 => Comment { id = 1, sender_id = 4, title = CSS, body = VV }

但是这显示的是:

0 => Comment { id = 10, sender_id = 5, title = Art, body = AA }

1 => Comment { id = 5, sender_id = 2, title = Foo, body = II }

2 => Comment { id = 3, sender_id = 2, title = You, body = WW }

3 => Comment { id = 2, sender_id = 3, title = Msc, body = LL }

4 => Comment { id = 0, sender_id = 4, title = CSS, body = NN }

  • 它提供最新的 cmets,但获取最早的。

  • 它完全跳过了用户 User-3 并显示了 User-2 的 2x 主题

  • 然后它在此处显示 User-3,在完成 User2 的 2x 主题之后,而不是在“Art”之后。

当我在上述查询中删除 ->paginate() 并尝试 ->toSql() 时,我收到:

"select * from `comments` where `receiver_id` = ? group by `title`, `sender_id` order by `id` desc"

我认为order by id desc 部分是在错误的位置创建的,所以这可能会导致问题。但是,我不确定。

另外,当我先交换->orderBy()->groupBy 的位置时,比如先->groupBy(),然后是->orderBy(),它会返回完全相同的结果。

最后,我尝试替换 ->orderBy('title', 'sender_id'),但结果还是一样。

我做错了什么或错过了什么?提前致谢。

【问题讨论】:

    标签: php mysql laravel pagination group-by


    【解决方案1】:

    您的问题是关于 mysql group by 在 order by 之前执行。

    解决方案应该有一个内部连接:

    Comment::join(\DB::raw('(select max(id) as id from comments group by sender_id) t'),function($join){
                      $join->on('comments.id', '=', 't.id');
    })->where('comments.receiver_id', Auth::user()->id)
      ->orderBy('id','desc')
      ->paginate(5);
    

    这里类似:

    MySQL order by before group by

    【讨论】:

    • 当我尝试您的示例时,出于某种原因,我收到了Column not found: 1054 Unknown column 't' in 'on clause'Column not found: 1054 Unknown column 't' in 'on clause' (SQL: select count(*) as aggregate from cmets` 内部连接(通过 sender_id 从 cmets 组中选择 max(id))t on comments.id = t 其中comments.receiver_id = 测试)`。我理解你的方法,但我不知道如何应用它,因为这会出错
    • @senty,fixed,miss as id statement
    • 谢谢!这让我向前迈出了一大步。它成功地将用户组合在一起。但是,我想要一个条目,其中 sender_id 和主题都是相同的。所以,我想为 ids 6&53&4 设置 2 个单独的元素,因为它们有不同的主题
    猜你喜欢
    • 2016-01-21
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 2017-03-25
    • 2021-10-13
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多