聚合查询字段支持DISTINCT

This commit is contained in:
thinkphp
2018-10-22 14:15:47 +08:00
parent b0514ee54c
commit 72b6c3ccaa

View File

@@ -558,11 +558,15 @@ class Query
*/ */
public function aggregate($aggregate, $field, $force = false) public function aggregate($aggregate, $field, $force = false)
{ {
if (0 === stripos($field, 'DISTINCT ')) {
list($distinct, $field) = explode(' ', $field);
}
if (!preg_match('/^[\w\.\*]+$/', $field)) { if (!preg_match('/^[\w\.\*]+$/', $field)) {
throw new Exception('not support data:' . $field); throw new Exception('not support data:' . $field);
} }
$result = $this->value($aggregate . '(' . $field . ') AS tp_' . strtolower($aggregate), 0, $force); $result = $this->value($aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $field . ') AS tp_' . strtolower($aggregate), 0, $force);
return $result; return $result;
} }