我正在使用 Laravel 5.5,我想知道如何在一次查询中获取每一列的最大值?
这样可以吗,还是要一一查询?
我试过下面的代码。
DB::table('family')->max('children')->max('salary');
当我只尝试 DB::table('family')->max('children')
时,效果很好。但是当我尝试 DB::table('family')->max('children')->max('salary')
时,出现以下错误
Call to a member function max() on string
最佳答案
您可以通过单个查询获得任何一个。
$result = DB::table("family as f")
->select([DB::raw('MAX(f.children) AS children_max'),DB::raw('MAX(f.salary) AS salary_max')])
->groupBy('f.id')
->first();
或者使用单独的->max()
方法。
$family = DB::table('family');
$max_children = $family->max('children');
$max_salary = $family->max('salary');
在 other answer 中获取更多详细信息在Laravel Docs
关于php - Laravel 获取每一列的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47854453/