📜  如何在 laravel 查询中使用并集和交集 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:53.398000             🧑  作者: Mango

代码示例1
$queryWithParent = SaleService::query()
    ->select(\DB::raw('DISTINCT ON (properties.parent_id) sale_services.*'))
    ->from('sale_services')
    ->join('properties', 'sale_services.property_id', '=', 'properties.id')
    ->whereNotNull('properties.parent_id')
    ->orderBy('properties.parent_id')
    ->orderBy('sale_services.index_range', 'desc');

$queryWithoutParent = SaleService::query()
    ->select(\DB::raw('sale_services.*'))
    ->join('properties', 'sale_services.property_id', '=', 'properties.id')
    ->whereNull('properties.parent_id');

$query = $queryWithParent->union($queryWithoutParent);