从RawQuery转换为Laravel 5 Query

时间:2016-11-24 06:37:18

标签: mysql 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();

1 个答案:

答案 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();