laravel query php how to get max value within a range
Asked Answered
K

3

5

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

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

Kerri answered 8/9, 2015 at 3:31 Comment(0)
S
6

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();
Sisal answered 8/9, 2015 at 3:41 Comment(1)
tnx it works, but i forget to mention another problem if its apropriate to add here, how can I get the max value where there is a decimal point, sory for adding another question, i'll just edit my question ^_^Kerri
W
2

Write query as below:

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

Reference: Laravel API

Wordage answered 8/9, 2015 at 3:48 Comment(0)
F
0

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

Fezzan answered 8/9, 2015 at 5:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.