2016-11-24 4 views
0

私のクエリは、私は5クエリをlaravelすることを変換したいは、クエリ

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 

あるRawQueryから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();