我的查询
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 :(得分: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();