简体   繁体   English

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

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

hello how do i get the max value of scores, where column ID range starts at 3-5 example table你好我如何获得分数的最大值,其中列 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

Try to use whereBetween hope 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();

Write query as below:编写查询如下:

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

Reference: Laravel API参考: Laravel API

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 文档

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

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