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