繁体   English   中英

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

[英]laravel query php how to get max value within a range

你好我如何获得分数的最大值,其中列 ID 范围从 3-5 示例表开始在此处输入图片说明

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

到目前为止我做了什么:

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

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

尝试使用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();

或者:

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

编写查询如下:

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

参考: Laravel API

使用这样的查询

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

供您参考,只需遵循Laravel 文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM