mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
FIX Exists statements can't remove GROUP BY if there's a HAVING clause as it might rely on an aggregate
This commit is contained in:
parent
91591373d6
commit
ef0af7ae9c
@ -466,12 +466,17 @@ class DataQuery
|
|||||||
$statement = $this->getFinalisedQuery();
|
$statement = $this->getFinalisedQuery();
|
||||||
$statement->setSelect('*');
|
$statement->setSelect('*');
|
||||||
|
|
||||||
// Clear limit, distinct, grouping, and order as it's not relevant for an exists query
|
// Clear limit, distinct, and order as it's not relevant for an exists query
|
||||||
$statement->setDistinct(false);
|
$statement->setDistinct(false);
|
||||||
$statement->setOrderBy(null);
|
$statement->setOrderBy(null);
|
||||||
$statement->setGroupBy(null);
|
|
||||||
$statement->setLimit(null);
|
$statement->setLimit(null);
|
||||||
|
|
||||||
|
// We can remove grouping if there's no "having" that might be relying on an aggregate
|
||||||
|
$having = $statement->getHaving();
|
||||||
|
if (empty($having)) {
|
||||||
|
$statement->setGroupBy(null);
|
||||||
|
}
|
||||||
|
|
||||||
// Wrap the whole thing in an "EXISTS"
|
// Wrap the whole thing in an "EXISTS"
|
||||||
$sql = 'SELECT EXISTS(' . $statement->sql($params) . ')';
|
$sql = 'SELECT EXISTS(' . $statement->sql($params) . ')';
|
||||||
$result = DB::prepared_query($sql, $params);
|
$result = DB::prepared_query($sql, $params);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user