如何使用laravel ORM执行以下操作?
select f.type, f.variety, f.price
from (
select type, min(price) as minprice
from fruits group by type
) as x inner join fruits as f on f.type = x.type and f.price = x.minprice;
答案 0 :(得分:0)
$subQuery = "(select type, min(price) as minprice from fruits group by type) as x";
$query = DB::table('fruits')::selectRaw( "fruits.type,fruits.variety,x.minprice" );
$query->join( DB::raw($subQuery), function ($join) {
$join->on ( 'fruits.type', '=', 'x.type' );
});
$data = $query->get();
我尚未测试代码,但希望它能工作。