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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32448857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:19:14  来源:igfitidea点击:

laravel query php how to get max value within a range

phpmysqllaravelquery-builder

提问by user4006175

hello how do i get the max value of scores, where column ID range starts at 3-5 example table enter image description here

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

I want to get the max value of scores, where column ID ranging from 3-5 , please help,

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

what I have done so far:

到目前为止我做了什么:

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

another problem is when i have a decimal points in the table when I used the max() function it gets the ID=5, which has a Score of 4.5, instead of ID=4 with a value of 4.6, tnx in advance

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

回答by aldrin27

Try to use whereBetweenhope this works:

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

回答by Ganesh Ghalame

Write query as below:

编写查询如下:

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

Reference:Laravel API

参考:Laravel API

回答by RaMeSh

Use query like this

使用这样的查询

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

For your reference just follow Laravel Documentation

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