php - laravel query php 如何获取一个范围内的最大值

标签 php mysql laravel query-builder

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

我想得到scores的最大值,其中column 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

最佳答案

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

关于php - laravel query php 如何获取一个范围内的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32448857/

相关文章:

MySQL:具有派生列的可更新 View ; "INSTEAD OF UPDATE"触发器的等效功能

php - 混合 OR & AND mysql 查询错误结果 (laravel)

php - 我在 select 中从 laravel 的数据库中得到了空白值

php - 如何在场景中为具有ajax验证的字段设置验证消息? - Yii2

php - 如何存储实体的动态/多个属性

php - MySQL RIGHT JOIN SUM() 从表 B 到表 A 的信息

mysql - PHP MySQL 分组问题

laravel - 在 Laravel 中根据用户名创建动态子域

php - 在 Laravel 中删除数据透视表中的行

php - 如何在 codeigniter 中调用另一个 Controller 中的一个 Controller 函数