如何将此sql查询转换为laravel查询生成器

时间:2017-01-17 10:05:50

标签: php mysql laravel laravel-5 eloquent

选择t.product_id,t.on_hand,t.created_at 来自table t, (SELECT MAX(purchase_id)为pId FROM table Group by product_id)tg 其中t.purchase_id = tg.pId

1 个答案:

答案 0 :(得分:0)

$result = DB::table('table as t')
->select('t.product_id', 't.on_hand', 't.created_at')
->join(DB::raw('(SELECT MAX(purchase_id) as pId FROM table Group by product_id) tg'), function($join) {
    $join->on('t.purchase_id', '=', 'tg.pId');
})->get();