使用Laravel Eloquent ORM获取GROUP中的最新值 [英] Get latest values in GROUP BY using Laravel Eloquent ORM
本文介绍了使用Laravel Eloquent ORM获取GROUP中的最新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图了解Laravel的口才ORM是如何工作的,并且正在查看以下MySQL查询:
I'm trying to understand how Laravel's Eloquent ORM works and was looking at the following MySQL query:
SELECT id, name, date FROM tablename GROUP BY name ORDER BY date
使用GROUP BY
总是返回最早的name
值.有没有办法返回最新值?
The use of GROUP BY
always returns the oldest values of name
. Is there a way to return the latest value instead?
推荐答案
尝试以下代码,
Tablename::select('id', 'name', DB::raw('max(date) as latest_date'))
->groupBy('name')
->orderBy('latest_date')
->get()
这篇关于使用Laravel Eloquent ORM获取GROUP中的最新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文