laravel查询php如何获取范围内的最大值 [英] laravel query php how to get max value within a range

查看:1147
本文介绍了laravel查询php如何获取范围内的最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我如何获得分数的最大值,其中列ID范围从3-5开始 示例表

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,

到目前为止我做了什么:

what I have done so far:

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

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

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

推荐答案

尝试使用whereBetween希望它能起作用:

Try to use whereBetween hope this works:

$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();

这篇关于laravel查询php如何获取范围内的最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆