1 回答

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
您正在執(zhí)行兩個(gè)查詢(xún),但這可以在一個(gè)查詢(xún)中完成。嘗試這個(gè)
Fish::with('photos', 'fishery', 'type')
->join(DB::raw('( SELECT type_id
, MAX(`length`) AS pb
FROM fishes
where user_id = 6
GROUP BY type_id
) AS f2'),function($join){
$join->on('fishes.type_id','=','f2.type_id')
->on('fishes.length','=','f2.pb');
})
->where('user_id', 6)->get();
代替
$sql = "
SELECT id
FROM fishes f1
JOIN **strong text**
( SELECT type_id
, MAX(`length`) AS pb
FROM fishes
where user_id = 6
GROUP BY type_id
) AS f2
ON f1.type_id = f2.type_id
and f2.pb = f1.length
where f1.user_id = 6
";
$sth = DB::getPdo()->prepare($sql);
$sth->execute();
$quy = $sth->fetchAll(\PDO::FETCH_COLUMN, 0);
$f = Fish::with('photos', 'fishery', 'type')
->where('user_id', 6)
->whereIn('id', $quy)->get();
- 1 回答
- 0 關(guān)注
- 161 瀏覽
添加回答
舉報(bào)