2016-11-24 73 views
0

我的查詢是轉換從RawQuery到Laravel 5查詢

select * from amos.equipment_oil 
inner join (
    select max(creation_date) as MaxDate 
    from amos.equipment_oil 
    group by DATE_FORMAT(creation_date, '%m-%d-%Y') 
) tm on equipment_oil.creation_date= tm.MaxDate 
where equipment_id = $id 
order by creation_date asc 

我想將其轉換成laravel 5查詢。請幫忙。 這是我到目前爲止。

$equipmentOil = EquipmentOil::where('equipment_id', $id)->orderBy('creation_date', 'asc')->get(); 

回答

0

試試這個:

$equipmentOi = 
DB::table('users') 
     ->select('equipment_oil.*') 
     ->join(DB::raw("(select max(creation_date) as MaxDate 
          from amos.equipment_oil 
          group by DATE_FORMAT(creation_date, '%m-%d-%Y')) tm"), function($join) { 
      $join->on('equipment_oil.creation_date', '=', 'tm.MaxDate'); 
     }) 
     ->where('equipment_id', $id) 
     ->orderBy('creation_date', 'asc') 
     ->get();