Browse Source

SqlCriteria - изменен расчет кол-ва записей при использовании GROUP BY

(In this thread: http://www.cyberforum.ru/mysql/thread681915.html
I have solution for my problem - USE COUNT(DISTINCT pid))
namespace
Alexander Demidov 10 years ago
parent
commit
aa9ac8dfe8
  1. 11
      Model/SqlCriteria.php

11
Model/SqlCriteria.php

@ -84,15 +84,22 @@ class SqlCriteria
{ {
$this->defineJoinExpressions($select); $this->defineJoinExpressions($select);
$sql_expression_backup = $this->sql_expression; $sql_expression_backup = $this->sql_expression;
$group_by_backup = $this->group_by;
$select_parent_query = 'COUNT(*) as count'; $select_parent_query = 'COUNT(*) as count';
$default_select = '*';
if ($this->group_by) {
$default_select = 'DISTINCT ' . $this->group_by[0];
}
$this->group_by = [];
if (!$this->sql_expression) { if (!$this->sql_expression) {
$this->sql_expression = 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM :table';
$this->sql_expression = 'SELECT COUNT(' . ($select ? $select : $default_select) . ') as count FROM :table';
$select_parent_query = 'x.count'; $select_parent_query = 'x.count';
} else { } else {
$this->sql_expression = preg_replace('#SELECT.+FROM#i', 'SELECT COUNT(' . ($select ? $select : '*') . ') as count FROM', $this->sql_expression);
$this->sql_expression = preg_replace('#SELECT.+FROM#i', 'SELECT COUNT(' . ($select ? $select : $default_select) . ') as count FROM', $this->sql_expression);
} }
$count = $this->model->count($select_parent_query, $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count'); $count = $this->model->count($select_parent_query, $this->where, null, $this->group_by, $this->sql_expression, $this->sql_expression_params)->fetchField('count');
$this->sql_expression = $sql_expression_backup; $this->sql_expression = $sql_expression_backup;
$this->group_by = $group_by_backup;
return $count; return $count;
} }

Loading…
Cancel
Save