简体   繁体   English

获取记录组中的最新值

[英]Get latest value in the group of records

I have this records table which I save weight, age and other biometrics in it.我有这个记录表,我在其中保存了体重、年龄和其他生物特征。 Now I want to fetch the latest value of every biometric for every day so I can draw a chart.现在我想获取每天每个生物特征的最新值,以便绘制图表。 The piece of code I'm using to fetch data from db before processing it is like the following:我用来在处理之前从 db 获取数据的代码如下:

 Records::where("user",$uid)->
     where("type", "biometric" )->  // record type
     where("fk", $bid )->    //biometric type
     where("datetime","<" ,$range->max)->
     where("datetime",">" ,$range->min)->
     groupBy( "datee" )->
     selectRaw("  max(amount)  as 'damount' , max(unit) as 'unit' ,   date(datetime) as 'datee'  ")->
     orderBy("datee","desc")->
     get(); 
    // !! Should change max values with the latest record in the group !!

Which datee is date of the day.哪个datee是当天的日期。 Say some users add more than one weight biometrics in a day so unit and damount should be the unit and value of lastest record of that day.假设有些用户一天添加了多个体重生物特征,因此单位和数量应该是当天最新记录的单位和值。

What can I use instead of max() to achieve this?我可以用什么代替 max() 来实现这一点?

Thanks in advance,提前致谢,

Hope this helps:希望这可以帮助:

Records::where("user",$uid)
    ->where(["type" => "biometric", "fk" => $bid])
    ->whereBetween(DB::raw("DATE('datetime')"), array($range->max, $range->min))
    ->groupBy("datetime")->latest()->get();

Can you try this你能试试这个

Records::where("user",$uid)
->where("type", "biometric")
->where("fk", $bid)  
->where("datetime","<" ,$range->max)
->where("datetime",">" ,$range->min)
->latest()
->groupBy('datetime')
->get();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM