2015-09-08 31 views
5

你好我如何得到的分數,其中列ID範圍開始於3-5 示例表 enter image description herelaravel查詢PHP如何一系列

的最大值中獲得最大的價值我想要得到的最大值

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

另一個問題是,當我在表一個小數點:得分,其中列ID從3-5 ,請大家幫幫忙,

是我迄今所做的當我用MAX()函數它獲取ID = 5,其具有4.5的分數,而不是ID = 4 4.6,TNX的值預先

回答

5

嘗試使用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(); 
+0

TNX它的工作原理,但我忘了如果apropriate這裏要補充,我怎樣才能提一個問題有一個小數點的最大值,sory添加另一個問題,我會編輯我的問題^ _^ – user4006175

1

寫入查詢如下(測試):

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

參考:Laravel API

0

使用這樣的查詢

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

供您參考只要按照Laravel Documentation

+0

- > get()將不起作用 –