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

时间:2015-09-08 03:31:26

标签: php mysql laravel query-builder

你好我如何得到分数的最大值,其中列ID范围从3-5开始 示例表 enter image description here

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

到目前为止我做了什么:

$max_scores_table= DB::table('scores_table')
->where('id', '>', 2)
->max('score');

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

3 个答案:

答案 0 :(得分: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();

OR:

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

答案 1 :(得分:1)

如下所示编写查询(已测试):

$max_scores_taable = DB::table('scores_table)
                     ->whereBetween('id',array(3,5))
                     ->max('score')

参考:Laravel API

答案 2 :(得分:0)

使用像这样的查询

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

仅供参考Laravel Documentation

供参考
相关问题