COUNT查询
@access public
@param string $field 字段名
@return integer|string
558904
public function count($field = '*')
    {
        if (!empty($this->options['group'])) {
            // 支持GROUP
            $options = $this->getOptions();
            $subSql  = $this->options($options)
                ->field('count(' . $field . ') AS think_count')
                ->bind($this->bind)
                ->buildSql();

            $query = $this->newQuery()->table([$subSql => '_group_count_']);

            if (!empty($options['fetch_sql'])) {
                $query->fetchSql(true);
            }

            $count = $query->aggregate('COUNT', '*');
        } else {
            $count = $this->aggregate('COUNT', $field);
        }

        return is_string($count) ? $count : (int) $count;
    }
170046
public function sum($field) {
        $this->build()->bindExpression('field', "sum({$field}) AS m");
        $data = $this->first();

        return $data ? $data['m'] : '';
    }
884439
public function inc($field, $num = 1, $return = Db::RETURN_ISOK)
	{
		$data = array ();
		// 参数都在field
		if (is_array($field))
		{
			// 数组参数，多个
			foreach ($field as $key => $value)
			{
				$f = $this->db->parseKeyword($key);
				$data = "{$f}={$f}+{$value}";
			}
		}
		else
		{
			// 单个单数
			$f = $this->db->parseKeyword($field);
			$data = "{$f}={$f}+{$num}";
		}
		$this->db->operationOption = $this->getOption();
		return $this->db->update($data, $return);
	}
796940
public function getLength($field)
    {
        $type = $field['type'];
        if (($i = strpos($type, '(')) !== false) {
            $j = strpos($type, ')', $i);

            return substr($type, $i + 1, $j - $i - 1);
        }
        return 0;
    }
360716
public function fetchCount($field = '')
    {
        $opts = $this->options;
        
        if ($field) {
            $opts['field'] = $field;
        } else {
            $opts['field'] = '*';
        }
        
        $sql = \wf\db\QueryBuilder::optionsToCountSql($opts);
        $num = $this->getDb()->getColumn($sql);
        
        return $num;
        
    }
18162
public function max($field)
    {
        $table = DataObject::getSchema()->tableForField($this->dataClass, $field);
        if (!$table) {
            return $this->aggregate("MAX(\"$field\")");
        }
        return $this->aggregate("MAX(\"$table\".\"$field\")");
    }
69827
public function select($field)
    {
        $fields = (is_array($field) ? $field : func_get_args());

        foreach ((array) $fields as $field) {
            $this->fields[$field] = 1;
        }
    }
747627
private function performCount($field = '*') {
        try {
            $this->checkDialect();
        } catch (DialectException $e) {
            $this->error          = $e->getMessage();
            $this->count          = 0;
            $this->errorSQL       = '';
            $this->errorValues    = null;
            $this->countperformed = true;

            return;
        }
        $this->count = false;
        $values      = new BindValues ();
        $fields [0]  = 'COUNT(' . $field . ')';
        $fields      = $this->prepareFields($fields, $values);
        $from        = $this->prepareFrom($this->sanitize($this->from));
        $joins       = $this->prepareJoins($this->sanitize($this->joins));
        $having      = $this->sanitize($this->having);
        $group       = $this->sanitize($this->group);
        $sql         = $this->dialect->getCountSelectSQL($fields, $from, $joins, $this->where, $having, $group, $values);
        if ($sql) {
            try {
                $this->options [ \PDO::ATTR_CURSOR ] = \PDO::CURSOR_SCROLL;
                $statement                           = $this->dialect->prepare($sql, $this->options);
                if ($values) {
                    foreach ($values as $value) {
                        list ($name, $val, $type, $field) = $value;
                        if ($this->whereData) {
                            $val = isset($this->whereData[ $field ]) ? $this->whereData[ $field ] : (isset($this->whereData[ $name ]) ? $this->whereData[ $name ] : $val);
                        }
                        if (!$statement->bindValue($name, $val, $type)) {
                            $this->countperformed = true;
                            $this->count          = 0;
                            $this->errorSQL       = $sql;
                            $this->errorValues    = $values->__toString();
                            $this->error          = 'can not bind the value ' . $val . '[' . $type . '] to the argument:' . $name;

                            return;
                        }
                    }
                }
                $rst = $statement->execute();
                QueryBuilder::addSqlCount();
                if ($rst) {
                    $resultSet = $statement->fetch(\PDO::FETCH_NUM);
                    $statement->closeCursor();
                    $this->count = intval($resultSet [0]);
                } else {
                    $this->dumpSQL($statement);
                }
            } catch (\PDOException $e) {
                $this->error       = $e->getMessage();
                $this->count       = 0;
                $this->errorSQL    = $sql;
                $this->errorValues = $values->__toString();
            }
        } else {
            $this->count       = 0;
            $this->errorSQL    = '';
            $this->errorValues = $values->__toString();
            $this->error       = 'can not generate the SQL';
        }
        $this->countperformed = true;
    }
272378
public function count()
    {
        $_table_name = $this->_get_table_name();
        $_where = ($this->where != '') ? ' WHERE '. $this->where : '';
        $_field = $this->field;
        $_join  = $this->join;

        //清理使用过的变量
        $this->where = '';
        $this->field = '*';
        $this->join = '';

        $this->conn('slave');//连接从数据库
        $this->sql = 'SELECT count('. $_field .') AS count FROM '.$_table_name.$_join.$_where;
        return $this->db->fetch_object( $this->sql )->count;
    }
1222012
public function aggregate($aggregate, $field)
    {
        $result = $this->cmd('aggregate', [$aggregate, $field]);
        return isset($result[0]['result'][0]['aggregate']) ? $result[0]['result'][0]['aggregate'] : 0;
    }
1053971
public function values($field)
    {
        $values = [];
        foreach ($this->distinct()->select([$field])->all() as $v) {
            $values[] = $v[$field];
        }

        return $values;
    }
190567
public function whereEmpty($field)
    {
        return $this->where(
            '$or',
            array(
                array($field => null),
                array($field => ''),
                array($field => array()),
                array($field => array('$exists' => false))
            )
        );
    }
160500
public function count($field = '*')
    {
        if ($field !== '*' && !in_array($field, $this->getFields())) {
            throw new \DomainException(__METHOD__ . '|Field is not allowed ! (field: ' . $field . ')');
        }

        $query = 'SELECT COUNT(' . $field . ') AS NB_RESULTS FROM ' . $this->table . ' ' . $this->getQueryWhere();

        $statement = $this->db->prepare($query);
        $statement->execute($this->binds);

        $this->clear();

        return (int) $statement->fetchColumn(0);
    }
1132829
public function count($field = '*')
    {
        $r = 0;
        foreach ($this->queries as $query) {
            $t = $query->count($field);
            $r += $t;
        }

        return $r;
    }
922086
public function field($field) {
        $field = is_array($field) ? $field : explode(',', $field);
        foreach ((array) $field as $k => $v) {
            $this->build()->bindExpression('field', $v);
        }

        return $this->connection;
    }
53753
protected function append($fields, string $field)
    {
        // 转换为字段列表
        $fields = $this->table->createFields($fields);

        // 如果是*,不用附加了
        if ($fields == '*') {
            return $fields;
        }

        // 分解为数组
        $fieldsArr = explode(',', $fields);

        // 如果要附加的字段已经在里面了
        if (in_array($field, $fieldsArr)) {
            return $fields;
        }

        // 附加一个字段
        $fieldsArr[] = $field;

        // 返回字段数组
        return $fieldsArr;
    }
332934
public function count($field = '*')
    {
        $copy = clone $this;

        $copy->_limit = null;
        $copy->_offset = null;
        $copy->_order = null;
        $copy->_aggregate['count'] = ['$sum' => 1];
        $r = $copy->execute();

        return $r ? $r[0]['count'] : 0;
    }