繁体   English   中英

多维分组排序和分页之前-Laravel 5

[英]Multi-Dimensional groupBy & orderBy before Paginate - Laravel 5

我想要做的是将具有相同主题的用户撰写的评论分组,并获得最新的(最近的)评论。 我尝试使用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和id是相同的顺序。 因此,我决定使用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 } (最新created_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 }

  • 它提供的是最新评论,但最早的评论。

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

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

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

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

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

另外,当我->groupBy->groupBy()一样交换->orderBy()->groupBy ,然后再->groupBy() ->orderBy() ,它将返回完全相同的结果。

最后,我尝试替换->orderBy('title', 'sender_id') ,但结果仍然相同。

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

您的问题是关于mysql group 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排序依据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM