laravel查询php如何获取范围内的最大值

use*_*175 5 php mysql query-builder laravel

您好,我如何获得分数的最大值,其中列ID范围从3-5示例表开始 在此输入图像描述

我想获得分数的最大值,其中列ID范围从3-5,请帮忙,

到目前为止我做了什么:

$max_scores_table= DB::table('scores_table')
->where('id', '>', 2)
->max('score');
Run Code Online (Sandbox Code Playgroud)

另一个问题是,当我使用max()函数时,表中有一个小数点,它得到ID = 5,得分为4.5,而不是ID = 4,值为4.6,tnx提前

ald*_*n27 5

尝试使用whereBetween希望这工作:

$max_scores_table= DB::table('scores_table')
    ->select(DB::raw('MAX(score) FROM scores_table as MaxScore'))
    ->whereBetween('id', array(3,5))
    ->where('score', 'MaxScore')
    ->get();
Run Code Online (Sandbox Code Playgroud)

要么:

$max_scores_table= DB::table('scores_table')
    ->whereBetween('id', array(3,5))
    ->max('score')
    ->get();
Run Code Online (Sandbox Code Playgroud)