Свойство | Type | Description | |
---|---|---|---|
$distinct | whether to select distinct rows of data only. If this is set true, the SELECT clause would be changed to SELECT DISTINCT. | ||
$from | the table(s) to be selected from. For example, ['user', 'post']. This is used to construct the FROM clause in a SQL statement. | ||
$groupBy | how to group the query results. For example, ['company', 'department']. This is used to construct the GROUP BY clause in a SQL statement. | ||
$having | the condition to be applied in the GROUP BY clause. It can be either a string or an array. Please refer to Query::where on how to specify the condition. | ||
$join | how to join with other tables. Each array element represents the specification of one join which has the following structure: php [$joinType, $tableName, $joinCondition] For example, php [ ['INNER JOIN', 'user', 'user.id = author_id'], ['LEFT JOIN', 'team', 'team.id = team_id'], ] | ||
$params | list of query parameter values indexed by parameter placeholders. For example, [':name' => 'Dan', ':age' => 31]. | ||
$select | the columns being selected. For example, ['id', 'name']. This is used to construct the SELECT clause in a SQL statement. If not set, it means selecting all columns. | ||
$selectOption | additional option that should be appended to the 'SELECT' keyword. For example, in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. | ||
$union | this is used to construct the UNION clause(s) in a SQL statement. Each array element is an array of the following structure: - query: either a string or a Query object representing a query - all: boolean, whether it should be UNION ALL or UNION |
Méthode | Description | |
---|---|---|
addGroupBy ( string | array $columns ) | Adds additional group-by columns to the existing ones. | |
addParams ( array $params ) | Adds additional parameters to be bound to the query. | |
addSelect ( string | array | yii\db\Expression $columns ) | Add more columns to the SELECT part of the query. | |
all ( |
Executes the query and returns all results as an array. | |
andFilterCompare ( string $name, string $value, string $defaultOperator = '=' ) | Adds a filtering condition for a specific column and allow the user to choose a filter operator. | |
andHaving ( string | array | yii\db\Expression $condition, array $params = [] ) | Adds an additional HAVING condition to the existing one. | |
andWhere ( string | array | yii\db\Expression $condition, array $params = [] ) | Adds an additional WHERE condition to the existing one. | |
average ( string $q, |
Returns the average of the specified column values. | |
batch ( integer $batchSize = 100, |
Starts a batch query. | |
column ( |
Executes the query and returns the first column of the result. | |
count ( string $q = '*', |
Returns the number of records. | |
create ( |
Creates a new Query object and copies its property values from an existing one. | |
createCommand ( |
Creates a DB command that can be used to execute this query. | |
distinct ( boolean $value = true ) | Sets the value indicating whether to SELECT DISTINCT or not. | |
each ( integer $batchSize = 100, |
Starts a batch query and retrieves data row by row. | |
exists ( |
Returns a value indicating whether the query result contains any row of data. | |
from ( string | array $tables ) | Sets the FROM part of the query. | |
groupBy ( string | array | yii\db\Expression $columns ) | Sets the GROUP BY part of the query. | |
having ( string | array | yii\db\Expression $condition, array $params = [] ) | Sets the HAVING part of the query. | |
innerJoin ( string | array $table, string | array $on = '', array $params = [] ) | Appends an INNER JOIN part to the query. | |
join ( string $type, string | array $table, string | array $on = '', array $params = [] ) | Appends a JOIN part to the query. | |
leftJoin ( string | array $table, string | array $on = '', array $params = [] ) | Appends a LEFT OUTER JOIN part to the query. | |
max ( string $q, |
Returns the maximum of the specified column values. | |
min ( string $q, |
Returns the minimum of the specified column values. | |
one ( |
Executes the query and returns a single row of result. | |
orHaving ( string | array | yii\db\Expression $condition, array $params = [] ) | Adds an additional HAVING condition to the existing one. | |
orWhere ( string | array | yii\db\Expression $condition, array $params = [] ) | Adds an additional WHERE condition to the existing one. | |
params ( array $params ) | Sets the parameters to be bound to the query. | |
populate ( array $rows ) : array | Converts the raw query results into the format as specified by this query. | |
prepare ( |
Prepares for building SQL. | |
rightJoin ( string | array $table, string | array $on = '', array $params = [] ) | Appends a RIGHT OUTER JOIN part to the query. | |
scalar ( |
Returns the query result as a scalar value. | |
select ( string | array | yii\db\Expression $columns, string $option = null ) | Sets the SELECT part of the query. | |
sum ( string $q, |
Returns the sum of the specified column values. | |
union ( string | |
Appends a SQL statement using UNION operator. | |
where ( string | array | yii\db\Expression $condition, array $params = [] ) | Sets the WHERE part of the query. |
Méthode | Description | |
---|---|---|
queryScalar ( string | yii\db\Expression $selectExpression, |
Queries a scalar value by setting [[select]] first. |
public addGroupBy ( string | array $columns ) | ||
$columns | string | array | additional columns to be grouped by. Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). Note that if your group-by is an expression containing commas, you should always use an array to represent the group-by information. Otherwise, the method will not be able to correctly determine the group-by columns. Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
public andFilterCompare ( string $name, string $value, string $defaultOperator = '=' ) | ||
$name | string | the column name. |
$value | string | the column value optionally prepended with the comparison operator. |
$defaultOperator | string | The operator to use, when no operator is given in `$value`. Defaults to `=`, performing an exact match. |
public average ( string $q, |
||
$q | string | the column name or expression. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | mixed | the average of the specified column values. |
public batch ( integer $batchSize = 100, |
||
$batchSize | integer | the number of records to be fetched in each batch. |
$db | the database connection. If not set, the "db" application component will be used. | |
Résultat | the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches. |
public column ( |
||
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | array | the first column of the query result. An empty array is returned if the query results in nothing. |
public count ( string $q = '*', |
||
$q | string | the COUNT expression. Defaults to '*'. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given (or null), the `db` application component will be used. | |
Résultat | integer | string | number of records. The result may be a string depending on the underlying database engine and to support integer values higher than a 32bit PHP integer can handle. |
public static create ( |
||
$from | the source query object | |
Résultat | the new Query object |
public createCommand ( |
||
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | the created DB command instance. |
public each ( integer $batchSize = 100, |
||
$batchSize | integer | the number of records to be fetched in each batch. |
$db | the database connection. If not set, the "db" application component will be used. | |
Résultat | the batch query result. It implements the [[\Iterator]] interface and can be traversed to retrieve the data in batches. |
public from ( string | array $tables ) | ||
$tables | string | array | the table(s) to be selected from. This can be either a string (e.g. `'user'`) or an array (e.g. `['user', 'profile']`) specifying one or several table names. Table names can contain schema prefixes (e.g. `'public.user'`) and/or table aliases (e.g. `'user u'`). The method will automatically quote the table names unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). When the tables are specified as an array, you may also use the array keys as the table aliases (if a table does not need alias, do not use a string key). Use a Query object to represent a sub-query. In this case, the corresponding array key will be used as the alias for the sub-query. Here are some examples: ```php // SELECT * FROM `user` `u`, `profile`; $query = (new \yii\db\Query)->from(['u' => 'user', 'profile']); // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; $subquery = (new \yii\db\Query)->from('user')->where(['active' => true]) $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); // subquery can also be a string with plain SQL wrapped in parenthesis // SELECT * FROM (SELECT * FROM `user` WHERE `active` = 1) `activeusers`; $subquery = "(SELECT * FROM `user` WHERE `active` = 1)"; $query = (new \yii\db\Query)->from(['activeusers' => $subquery]); ``` |
public groupBy ( string | array | yii\db\Expression $columns ) | ||
$columns | string | array | yii\db\Expression | the columns to be grouped by. Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). Note that if your group-by is an expression containing commas, you should always use an array to represent the group-by information. Otherwise, the method will not be able to correctly determine the group-by columns. Since version 2.0.7, an [[Expression]] object can be passed to specify the GROUP BY part explicitly in plain SQL. |
public having ( string | array | yii\db\Expression $condition, array $params = [] ) | ||
$condition | string | array | yii\db\Expression | the conditions to be put after HAVING. Please refer to [[where()]] on how to specify this parameter. |
$params | array | the parameters (name => value) to be bound to the query. |
public innerJoin ( string | array $table, string | array $on = '', array $params = [] ) | ||
$table | string | array | the table to be joined. Use a string to represent the name of the table to be joined. The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a [[Query]] object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string | array | the join condition that should appear in the ON part. Please refer to [[join()]] on how to specify this parameter. |
$params | array | the parameters (name => value) to be bound to the query. |
public join ( string $type, string | array $table, string | array $on = '', array $params = [] ) | ||
$type | string | the type of join, such as INNER JOIN, LEFT JOIN. |
$table | string | array | the table to be joined. Use a string to represent the name of the table to be joined. The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a [[Query]] object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string | array | the join condition that should appear in the ON part. Please refer to [[where()]] on how to specify this parameter. Note that the array format of [[where()]] is designed to match columns to values instead of columns to columns, so the following would **not** work as expected: `['post.author_id' => 'user.id']`, it would match the `post.author_id` column value against the string `'user.id'`. It is recommended to use the string syntax here which is more suited for a join: ```php 'post.author_id = user.id' ``` |
$params | array | the parameters (name => value) to be bound to the query. |
public leftJoin ( string | array $table, string | array $on = '', array $params = [] ) | ||
$table | string | array | the table to be joined. Use a string to represent the name of the table to be joined. The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a [[Query]] object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string | array | the join condition that should appear in the ON part. Please refer to [[join()]] on how to specify this parameter. |
$params | array | the parameters (name => value) to be bound to the query |
public max ( string $q, |
||
$q | string | the column name or expression. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | mixed | the maximum of the specified column values. |
public min ( string $q, |
||
$q | string | the column name or expression. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | mixed | the minimum of the specified column values. |
public one ( |
||
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | array | boolean | the first row (in terms of an array) of the query result. False is returned if the query results in nothing. |
public prepare ( |
||
$builder |
public rightJoin ( string | array $table, string | array $on = '', array $params = [] ) | ||
$table | string | array | the table to be joined. Use a string to represent the name of the table to be joined. The table name can contain a schema prefix (e.g. 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it contains some parenthesis (which means the table is given as a sub-query or DB expression). Use an array to represent joining with a sub-query. The array must contain only one element. The value must be a [[Query]] object representing the sub-query while the corresponding key represents the alias for the sub-query. |
$on | string | array | the join condition that should appear in the ON part. Please refer to [[join()]] on how to specify this parameter. |
$params | array | the parameters (name => value) to be bound to the query |
public scalar ( |
||
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | string | null | false | the value of the first column in the first row of the query result. False is returned if the query result is empty. |
public select ( string | array | yii\db\Expression $columns, string $option = null ) | ||
$columns | string | array | yii\db\Expression | the columns to be selected. Columns can be specified in either a string (e.g. "id, name") or an array (e.g. ['id', 'name']). Columns can be prefixed with table names (e.g. "user.id") and/or contain column aliases (e.g. "user.id AS user_id"). The method will automatically quote the column names unless a column contains some parenthesis (which means the column contains a DB expression). A DB expression may also be passed in form of an [[Expression]] object. Note that if you are selecting an expression like `CONCAT(first_name, ' ', last_name)`, you should use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts. When the columns are specified as an array, you may also use array keys as the column aliases (if a column does not need alias, do not use a string key). Starting from version 2.0.1, you may also select sub-queries as columns by specifying each such column as a `Query` instance representing the sub-query. |
$option | string | additional option that should be appended to the 'SELECT' keyword. For example, in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
public sum ( string $q, |
||
$q | string | the column name or expression. Make sure you properly [quote](guide:db-dao#quoting-table-and-column-names) column names in the expression. |
$db | the database connection used to generate the SQL statement. If this parameter is not given, the `db` application component will be used. | |
Résultat | mixed | the sum of the specified column values. |
public $distinct |
public $from |
public $groupBy |
public $having |
public $join |
public $params |
public $select |
public $selectOption |
public $union |