【问题标题】:Querying the best result in terms of sum of column's rows in Eloquent ORM laravel?在 Eloquent ORM laravel 中查询列行总和的最佳结果?
【发布时间】:2017-03-29 14:14:47
【问题描述】:

我有一张这样的桌子

现在我首先查询相同 temperaturzeit 的结果,例如

(select * from table where temperatur= array..  AND zeit=array...). 

例如,我得到如下结果:

一个温度和 zeit= 85,30,另一个 85,45..

现在根据 (aussehen,farbe,deformation,geruch,texture) 列的总和,我想得到最好的结果。

例如,匹配列的最终总和是(14),另一个匹配是(17)我想得到最好的结果是 14..

如何在 laravel orm 中做到这一点?

对于整个查询,我使用了这个

orderBy( DB::raw('(aussehen) + (farbe)+(deformation)+(geruch)+(texture)'))->limit(1)->get();

但知道我想比较 sum 的结果..

我希望我已经正确解释了我的问题..

【问题讨论】:

  • 有人吗? ......

标签: php mysql laravel orm eloquent


【解决方案1】:

试试这个:

$result = DB::('table')
     ->select(DB::raw('*, (aussehen) + (farbe)+(deformation)+(geruch)+(texture) as sum'))
     ->where('temperature', '=', $temp) //variable passed to function
     ->where('zeit', '=', $zeit) // variable passed to function
     ->orderBy('sum', 'asc')
     ->first();   

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-22
    • 1970-01-01
    • 2015-02-27
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多