【问题标题】:laravel latest record fetch with groupBy Clause [duplicate]laravel 使用 groupBy 子句获取最新记录 [重复]
【发布时间】:2021-03-15 06:37:05
【问题描述】:

在 LARAVEL 中,我的用户有很多带有模型测试 ID 的答案。现在我想要 model_test_id 组的最后创建的答案。这是我编写的 LARAVEL 代码,但它给了我在组中找到的第一个值,然后按 created_at 排序。如何使用 group 子句获取最近的数据?

$answers = auth()->user()->answers()->groupBy('model_test_id')->orderBy('created_at', 'DESC')->get();
answers
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid                                 | user_id | model_test_id | created_at          |
+----+--------------------------------------+---------+---------------+---------------------+
|  1 | 1ffcb80a-c426-4424-9fda-1cadf573289f |       1 |            11 | 2021-03-08 12:13:07 |
|  2 | 8d23ad83-0913-4beb-91f3-4851384c52d3 |       1 |            12 | 2021-03-08 12:18:33 |
| 18 | c60066e4-ea15-4b5a-82f0-d635b912bad1 |       1 |             5 | 2021-03-08 10:18:20 |
| 20 | 499e02b3-571f-444f-be34-cfc7499d3a77 |       1 |            11 | 2021-03-10 14:37:43 |
| 21 | 962f892c-83c3-4f1f-88cf-17f0a475ac07 |       1 |            12 | 2021-03-10 16:21:45 |
| 22 | ebd713f5-fdf2-4dc6-9be1-4f45a8ee71dd |       1 |             5 | 2021-03-10 17:03:07 |
+----+--------------------------------------+---------+---------------+---------------------+
output data
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid                                 | user_id | model_test_id | created_at          |
+----+--------------------------------------+---------+---------------+---------------------+
|  2 | 8d23ad83-0913-4beb-91f3-4851384c52d3 |       1 |            12 | 2021-03-08 12:18:33 |
|  1 | 1ffcb80a-c426-4424-9fda-1cadf573289f |       1 |            11 | 2021-03-08 12:13:07 |
| 18 | c60066e4-ea15-4b5a-82f0-d635b912bad1 |       1 |             5 | 2021-03-08 10:18:20 |
+----+--------------------------------------+---------+---------------+---------------------+
i want this output
+----+--------------------------------------+---------+---------------+---------------------+
| id | uuid                                 | user_id | model_test_id | created_at          |
+----+--------------------------------------+---------+---------------+---------------------+
| 22 | ebd713f5-fdf2-4dc6-9be1-4f45a8ee71dd |       1 |             5 | 2021-03-10 17:03:07 |
| 21 | 962f892c-83c3-4f1f-88cf-17f0a475ac07 |       1 |            12 | 2021-03-10 16:21:45 |
| 20 | 499e02b3-571f-444f-be34-cfc7499d3a77 |       1 |            11 | 2021-03-10 14:37:43 |
+----+--------------------------------------+---------+---------------+---------------------+

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

您可以先订购:

$answer = auth()->user()->answers()->orderBy('created_at', 'DESC')->get();

然后group他们。

$answers = DB::table(DB::raw("({$answer->toSql()}) as answers"))
->groupBy('model_test_id')
->get();

【讨论】:

  • 我不想要第一条记录。
  • 它给你最新的记录,\所以你想要所有登录用户的答案还是其中一些?
  • 查看 输出数据 created_at 列所有数据都是旧的,但我想要最近 created_at 数据,正如我在输出中提到的那样。
  • orderBygroupBy的位置怎么样,先获取数据排序再分组?
  • 这是我之前得到的相同数据。没有变化
猜你喜欢
  • 2018-03-04
  • 1970-01-01
  • 2019-06-13
  • 2013-03-30
  • 2021-03-05
  • 2013-06-27
  • 1970-01-01
  • 1970-01-01
  • 2018-04-22
相关资源
最近更新 更多