我有一個關系,我根據類別選擇所有行,但是如果它們在子查詢中,我需要排除其中的一些。
/** @var \Illuminate\Database\Query\Builder $images */
$images = $vehicle->images()
->whereIn('image_category', $website->image_categories)
->orderBy('seq', 'ASC');
$images->whereNotIn('id', static function ($q) {
return $q->select('id')
->whereIn('image_category', [0, 99])
->groupBy('seq')
->having(DB::raw('count(`seq`)'), '>', 1);
});
dd($images->toSql(), $images->getBindings());
所以上面是我的代碼,幾乎可以按我的意愿作業,但是似乎該$q變數在查詢中沒有表名,下面是輸出的查詢:
select
*
from
`vehicle_images`
where
`vehicle_images`.`vehicle_id` = ?
and `vehicle_images`.`vehicle_id` is not null
and `image_category` in (?, ?)
and `id` not in (
select
`id`
where
`image_category` in (?, ?)
group by
`seq`
having
count(`seq`) > ?
)
order by
`seq` asc
這是關系:
public function images()
{
return $this->hasMany(VehicleImage::class);
}
uj5u.com熱心網友回復:
您可以指定要使用的表。
$images->whereNotIn('id', static function ($q) {
return $q->select('id')->from('{CORRECT_TABLE_NAME_HERE}')
->whereIn('image_category', [0, 99])
->groupBy('seq')
->having(DB::raw('count(`seq`)'), '>', 1);
});
我不知道表名到底應該是什么,因此是占位符。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/445189.html
上一篇:MariaDBSQL:針對單個源的多個結果的動態列而不是行
下一篇:查找應該有2個的單個條目
