laravel query php how to get max value within a range

▼魔方 西西 提交于 2020-01-01 10:28:50

问题


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

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');

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


回答1:


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



回答2:


Write query as below:

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

Reference: Laravel API




回答3:


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



来源:https://stackoverflow.com/questions/32448857/laravel-query-php-how-to-get-max-value-within-a-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!