簡體   English   中英

嗨,我想在Laravel中獲得leftJoin或LIMIT 1中的MAX值,我該怎么辦?

[英]Hi i want to get MAX value in leftJoin or LIMIT 1 in Laravel, how can i do it?

$almacenes = Almacenes::with(['producto'])->
                 leftJoin('albaranes_lineas',function ($k) use ($request){
                     $k->on('almacen.id_producto','=','albaranes_lineas.id_producto');


                 })->where(function ($q) use ($request) {

                $q->where('almacen.id_usuario', $request->usuario);
                $q->where('almacen.id_local', $request->local);
                $q->where('almacen.tipo', $request->tipo);
                $q->whereRaw('HOUR(almacen.created_at) = ?', $request->hora);
                $q->whereDate('almacen.created_at', '=', $request->fecha);
                //$q->where('inventario', 1);


            })->get();

如何將值限制為“ 1”? 我想獲取表“ albaranes_lineas”中每一行的最后一個字段。

$maxValue = Almacenes::with(['producto'])->
                 leftJoin('albaranes_lineas',function ($k) use ($request){
                     $k->on('almacen.id_producto','=','albaranes_lineas.id_producto');
                     $k->where('albaranes_lineas.id',821);
                 })->max('table_name.column_name');

當您使用max('column_name') ,它將返回一個計數值。 在這種情況下, $maxValue將保存您的列數。 這是laravel中可用的聚合函數。

您不能調用max()->limit() ,就像在整數值中調用->limit() ,它會引發錯誤:

在整數上調用成員函數limit()

但是,如果要獲得特定條件的最大值,則需要使用group by子句。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM