php - Distinct union aren't unique
one text
I added same leftJoin to 2 queries:
$query1->leftJoin(
$this->table1,
"$this->table1.since",
'=',
DB::raw(
"(select min(`since`) from $this->table1
WHERE $this->table1.product_uuid = $this->table2.uuid
AND $this->table1.till >= '$dateStr')"
)
);
$query2->leftJoin(
$this->table1,
"$this->table1.since",
'=',
DB::raw(
"(select min(`since`) from $this->table1
WHERE $this->table1.product_uuid = $this->table2.uuid
AND $this->table1.till >= '$dateStr')"
)
);
When I using distinct, products aren't unique:
$finalQuery = $query1->union($query2);
$products = $finalQuery->distinct();
$finalQuery = $this->applyPaginationLimits($finalQuery, $limit, $offset);
$products = $finalQuery->get();
$products->count() == $products->unique('uuid')->count() \\false
I tried to change distinct() to groupBy() but I would have to change code in other places, table2.order which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
. I can't remove duplicates on collection $products becouse of pagination. Is there any solution to keep the open/close rule?