diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 7952393fb8be..92c47c34bff0 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4904,7 +4904,7 @@ public function searchQuery( // MySQL expect the columns present in GROUP BY, must be present in SELECT clause and that results into error, needless to have other columns. // 2. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT // clause. This will impact the search query output. - $disableFullGroupByMode = ($sortByChar || !empty($groupByCols)); + $disableFullGroupByMode = ($sortByChar || !empty($groupByCols) || $groupContacts); if ($disableFullGroupByMode) { CRM_Core_DAO::disableFullGroupByMode(); @@ -4984,7 +4984,7 @@ public function getCachedContacts($cids, $includeContactIds) { $select .= sprintf(", (%s) AS _wgt", $this->createSqlCase('contact_a.id', $cids)); $where .= sprintf(' AND contact_a.id IN (%s)', implode(',', $cids)); $order = 'ORDER BY _wgt'; - $groupBy = ''; + $groupBy = $this->_useGroupBy ? ' GROUP BY contact_a.id' : ''; $limit = ''; $query = "$select $from $where $groupBy $order $limit"; diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index 989716ddb440..12f73b41d112 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1043,7 +1043,7 @@ public function fillupPrevNextCache($sort, $cacheKey, $start = 0, $end = self::C $selectSQL = "SELECT DISTINCT '$cacheKey', contact_a.id, contact_a.sort_name"; - $sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $selectSQL, $sql); + $sql = str_ireplace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $selectSQL, $sql); try { Civi::service('prevnext')->fillWithSql($cacheKey, $sql); }