Returns the distinct values for a given key.

@param string $field field to use
@param mixed  $query match query

@return array array of distinct values
549412
public function distinct($key, $query = [])
    {
        if ($query instanceof Closure) {
            // Store the callback
            $callback = $query;

            // Create a new Where filter.
            $query = new Query\Where();

            // trigger callback
            $callback($query);
        }

        if ($query instanceof Query\Where) {
            // Get the filter.
            $query = $query->getWhere();
        }

        return $this->collection->distinct($key, $query);
    }
1172328
public static function fetchOneValue(string $field, $where = null, $orderField = null, $orderDirection = null)
    {
        $select = static::select()->field($field, 'key_field')->limit(0, 1);

        if ($where !== null) {
            if ($where instanceof Where) {
                $select->where($where);
            } else if (is_array($where)) {
                foreach ($where as $field => $value) {
                    $select->where($field, $value);
                }
            } else if (!is_array(static::$primaryKey) && (is_numeric($where) || is_string($where))) {
                $select->where(static::$primaryKey, $where);
            }
        }

        if ($orderField !== null) {
            $select->orderBy($orderField, $orderDirection);
        }

        $records = $select->fetchAll();
        if (count($records) == 0) {
            return null;
        }

        return $records[0]['key_field'];
    }
212349
def values(self, *args: str, **kwargs: str) -> "ValuesQuery":
        
        fields_for_select = {}  # type: Dict[str, str]
        for field in args:
            if field in fields_for_select:
                raise FieldError("Duplicate key {}".format(field))
            fields_for_select[field] = field

        for return_as, field in kwargs.items():
            if return_as in fields_for_select:
                raise FieldError("Duplicate key {}".format(return_as))
            fields_for_select[return_as] = field

        return ValuesQuery(
            db=self._db,
            model=self.model,
            q_objects=self._q_objects,
            fields_for_select=fields_for_select,
            distinct=self._distinct,
            limit=self._limit,
            offset=self._offset,
            orderings=self._orderings,
            annotations=self._annotations,
            custom_filters=self._custom_filters,
        )
335917
public function setFieldQuery(string $field, string $query): self
    {
        return $this->setFieldParam($field, 'query', $query);
    }
504545
public function keyResultsBy($field)
    {
        $results = clone $this->results;
        $this->results = new Collection;
        foreach ($results as $r) {
            $this->addRecord($r, $field);
        }
    }
405843
public function formatFields(Query $query) {
        $joins = $query->getJoins();
        $type = $query->getType();

        switch ($type) {
            case Query::INSERT:
            case Query::MULTI_INSERT:
                $fields = $query->getData();

                if (empty($fields)) {
                    throw new InvalidQueryException('Missing field data for insert query');
                }

                if ($type === Query::MULTI_INSERT) {
                    $fields = $fields[0];
                }

                return '(' . $this->quoteList(array_keys($fields)) . ')';
            break;

            case Query::SELECT:
                $fields = $query->getFields();

                if ($joins) {
                    $columns = $this->formatSelectFields($fields, $query->getRepository()->getAlias());

                    foreach ($joins as $join) {
                        $fields = $join->getFields();

                        if (empty($fields)) {
                            throw new InvalidQueryException('Missing field data for join query');
                        }

                        $columns = array_merge($columns, $this->formatSelectFields($fields, $join->getAlias()));
                    }
                } else {
                    $columns = $this->formatSelectFields($fields);
                }

                return implode(', ', $columns);
            break;

            case Query::UPDATE:
                $fields = $query->getData();

                if (empty($fields)) {
                    throw new InvalidQueryException('Missing field data for update query');
                }

                if ($joins) {
                    $values = $this->formatUpdateFields($fields, $query->getRepository()->getAlias());

                    foreach ($joins as $join) {
                        $values = array_merge($values, $this->formatUpdateFields($join->getFields(), $join->getAlias()));
                    }
                } else {
                    $values = $this->formatUpdateFields($fields);
                }

                return implode(', ', $values);
            break;

            case Query::CREATE_INDEX:
                $fields = $query->getData();
                $columns = [];

                foreach ($fields as $column => $data) {
                    if (is_numeric($column)) {
                        $column = $data;
                        $data = [];
                    }

                    if (is_numeric($data)) {
                        $data = ['length' => $data, 'order' => '', 'collate' => ''];
                    } else if (is_string($data)) {
                        $data = ['length' => '', 'order' => $data, 'collate' => ''];
                    }

                    $column = $this->quote($column);

                    if (!empty($data['length'])) {
                        $column .= sprintf($this->getClause(self::GROUP), $data['length']);
                    }

                    if (!empty($data['collate'])) {
                        $column .= ' ' . sprintf($this->getClause(self::COLLATE), $data['collate']);
                    }

                    if (!empty($data['order'])) {
                        $column .= ' ' . $this->getKeyword($data['order']);
                    }

                    $columns[] = $column;
                }

                return implode(', ', $columns);
            break;
        }

        return '';
    }
717536
public function getArrays($query)
    {
        $this->statement = $this->db->query($query);
        if ($this->statement) {
            $result =  $this->statement->fetchAll(\PDO::FETCH_ASSOC);
            if ($result) {
                return $result;
            }
        }
        return false;
    }
875212
public function getMap(string $field, string $key = 'id', Condition $condition = null): array
    {
        $result = [];
        $select = $this->selectQuery($condition);
        $select->select(['t1.' . $field, 't1.' . $key]);
        if ($rows = $select->execute()->fetchAll(\PDO::FETCH_ASSOC)) {
            foreach ($rows as $row) {
                $this->unserializeFields($row);
                $result[$row[$key]] = $row[$field];
            }
        }

        return $result;
    }
79261
public function getResults($query)
    {
        $parameters = [];

        if (is_object($query) && $query instanceof SearchQuery) {
            $query = $query->toArray();
        }

        if (is_array($query)) {
            $collection = new Collection($query);

            $query = $collection->pull('query', '');

            $parameters = $collection->toArray();
        }

        return $this->index->search($query, $parameters);
    }
596189
public function find($query, $fields = ['username', 'email']): UserInterface
    {
        foreach ((array)$fields as $field) {
            if ($field === 'key') {
                $user = $this->get($query);
            } elseif ($field === 'storage_key') {
                $user = $this->withKeyField('storage_key')->get($query);
            } elseif ($field === 'flex_key') {
                $user = $this->withKeyField('flex_key')->get($query);
            } elseif ($field === 'email') {
                $user = $this->withKeyField('email')->get($query);
            } elseif ($field === 'username') {
                $user = $this->get(mb_strtolower($query));
            } else {
                $user = $this->__call('find', [$query, $field]);
            }
            if ($user) {
                return $user;
            }
        }

        return $this->load('');
    }
662651
def get_db_distinct(queryset, field, func, **params):
    
    while True:
        try:
            value = func(**params)
            queryset.get(**{field: value})
        except ObjectDoesNotExist:
            break
    return value
354988
public function aggregate(Query $query, $aggregate, $field)
    {
        if (is_string($field) && 0 === stripos($field, 'DISTINCT ')) {
            list($distinct, $field) = explode(' ', $field);
        }

        $field = $aggregate . '(' . (!empty($distinct) ? 'DISTINCT ' : '') . $this->builder->parseKey($query, $field, true) . ') AS tp_' . strtolower($aggregate);

        return $this->value($query, $field, 0);
    }
330768
function queryColumn($query)
  {
    $data = [];
    $res = $this->q($query);
    while ($row = $res->fetch_row())
    {
      if ($res->field_count == 1)
        $data[] = $row[0];
      else
        $data[$row[0]] = $row[1];
    }
    $res->close();
    return $data;
  }
457929
public function findColumn($field) {
        $rows = $this->select($field)->findAll();

        //false or null
        if (!$rows) {
            return $rows;
        }

        $columnValues = [];
        foreach ($rows as $row) {
            $columnValues[] = $row[$field];
        }
        return $columnValues;
    }
1150597
protected static function query($fields)
    {
        $result = Query::from(static::$table);

        $result = $result->where();

        foreach ($fields as $key => $value) {
            $result = $result->andWhere($key, '=', $value);
        }

        return $result->get();
    }
321126
protected function doFindOne(array $query, array $fields)
    {
        $riakCollection = $this->riakCollection;
        return $this->retry(function() use ($riakCollection, $query, $fields) {
            return $riakCollection->findOne($query, $fields);
        });
    }
201664
protected function getRelationshipArray($query, $field_parameters)
    {
        // Relationship data is stored to prevent re-querying
        if (! $this->relationshipData) {
            $key_column = isset($field_parameters['key_column']) ? $field_parameters['key_column'] : 'id';
            $value_column = isset($field_parameters['value_column']) ? $field_parameters['value_column'] : $key_column;

            $this->relationshipData = $query->pluck($value_column, $key_column)->toArray();
        }

        return $this->relationshipData;
    }