【发布时间】: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