【问题标题】:laravel query php how to get max value within a rangelaravel查询php如何获取范围内的最大值
【发布时间】:2015-12-03 14:39:39
【问题描述】:

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

我想获取分数的最大值,其中列 ID 范围为 3-5 ,请帮忙,

到目前为止我做了什么:

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

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

【问题讨论】:

    标签: php mysql laravel query-builder


    【解决方案1】:

    尝试使用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();
    

    或者:

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

    【讨论】:

    • tnx 它有效,但我忘了提另一个问题,如果它适合在这里添加,我怎样才能获得有小数点的最大值,抱歉添加另一个问题,我只是编辑我的问题^_^
    【解决方案2】:

    如下编写查询:

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

    参考:Laravel API

    【讨论】:

      【解决方案3】:

      使用这样的查询

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

      请关注Laravel Documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-01
        • 1970-01-01
        • 2014-03-11
        • 2020-10-16
        • 1970-01-01
        • 2016-07-09
        • 2016-09-05
        相关资源
        最近更新 更多