区分数据库中的最新数据
Distinct most recent data from database
我正在将数据存储在我的数据库中。存储的数据如下所示
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:22:39
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
3 | January | 2017-01-30 13:25:33
在我的 Controller 中,我试图检索不同的 upload_month,但为每个获取最新插入的版本。目前我正在尝试
$uploadedFile = UploadedFile::groupBy('upload_month')->orderBy('created_at', 'desc')->get();
问题是这会返回以下内容
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:22:39
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
因此,对于一月份的记录,它提供的是旧版本。如果我将其更改为 ->orderBy('created_at', 'asc')
,它会 returns 相同的记录,但 2 月是第一行。
本质上我追求的是这个
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:25:33
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
我怎样才能做到这一点?
谢谢
您应该 GROUP BY
所有您想要 select 的字段,而不是只有一个。本文解释问题:https://www.psce.com/blog/2012/05/15/mysql-mistakes-do-you-use-group-by-correctly/
在这种情况下,正确的 SQL 查询是:
SELECT id, upload_month, created_at
FROM uplodaded_file
JOIN (SELECT upload_month, MAX(created_at) created_at
FROM uplodaded_file
GROUP BY upload_month) months
ON upload_month = months.upload_month
AND created_at = months.created_at
eloquent 版本有点棘手。在这种情况下最好使用原始查询。
您应该使用 latest() 方法而不是 orderBy:
UploadedFile::latest()->distinct()->get();
我遇到了这个问题,然后就这样解决了。
UploadedFile::select(DB::raw('upload_month, MAX(created_at) as latest_date'))
->groupBy('upload_month')->orderBy('latest_date', 'desc')->get()
我正在将数据存储在我的数据库中。存储的数据如下所示
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:22:39
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
3 | January | 2017-01-30 13:25:33
在我的 Controller 中,我试图检索不同的 upload_month,但为每个获取最新插入的版本。目前我正在尝试
$uploadedFile = UploadedFile::groupBy('upload_month')->orderBy('created_at', 'desc')->get();
问题是这会返回以下内容
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:22:39
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
因此,对于一月份的记录,它提供的是旧版本。如果我将其更改为 ->orderBy('created_at', 'asc')
,它会 returns 相同的记录,但 2 月是第一行。
本质上我追求的是这个
id | upload_month | created_at
-----------------------------------------
1 | January | 2017-01-30 13:25:33
-----------------------------------------
2 | Febuary | 2017-01-30 13:23:42
-----------------------------------------
我怎样才能做到这一点?
谢谢
您应该 GROUP BY
所有您想要 select 的字段,而不是只有一个。本文解释问题:https://www.psce.com/blog/2012/05/15/mysql-mistakes-do-you-use-group-by-correctly/
在这种情况下,正确的 SQL 查询是:
SELECT id, upload_month, created_at
FROM uplodaded_file
JOIN (SELECT upload_month, MAX(created_at) created_at
FROM uplodaded_file
GROUP BY upload_month) months
ON upload_month = months.upload_month
AND created_at = months.created_at
eloquent 版本有点棘手。在这种情况下最好使用原始查询。
您应该使用 latest() 方法而不是 orderBy:
UploadedFile::latest()->distinct()->get();
我遇到了这个问题,然后就这样解决了。
UploadedFile::select(DB::raw('upload_month, MAX(created_at) as latest_date'))
->groupBy('upload_month')->orderBy('latest_date', 'desc')->get()