简体   繁体   English

如何将此 sql 查询转换为 Laravel 查询构建器

[英]How to Convert this sql query into laravel query builder

select t.product_id,t.on_hand,t.created_at from table t, (SELECT MAX(purchase_id) as pId FROM table Group by product_id) tg where t.purchase_id = tg.pId

$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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM