Interface Yiisoft\Db\Query\QueryPartsInterface
| Implemented by | Yiisoft\Db\Query\QueryInterface |
|---|
This interface defines a set of methods to create and manipulate the different parts of a database query, such as the {@see addGroupBy()}, {@see addSelect()}, {@see addOrderBy()}, {@see andFilterCompare()}, etc.
{@see \Yiisoft\Db\Query\Query} uses these methods to build and manipulate SQL statements.
Psalm Types
| Name | Value |
|---|---|
| SelectValue | array<array-key, Yiisoft\Db\Expression\ExpressionInterface|scalar> |
Public Methods
| Method | Description | Defined By |
|---|---|---|
| addFor() | Adds more FOR parts to the existing ones. |
Yiisoft\Db\Query\QueryPartsInterface |
| addGroupBy() | Adds more group-by columns to the existing ones. | Yiisoft\Db\Query\QueryPartsInterface |
| addOrderBy() | Adds more ORDER BY columns to the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| addSelect() | Add more columns to the SELECT part of the query. | Yiisoft\Db\Query\QueryPartsInterface |
| addWithQuery() | Prepends an SQL statement using WITH syntax. |
Yiisoft\Db\Query\QueryPartsInterface |
| andFilterCompare() | Adds a filtering condition for a specific column and allow the user to choose a filter operator. | Yiisoft\Db\Query\QueryPartsInterface |
| andFilterHaving() | Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}. | Yiisoft\Db\Query\QueryPartsInterface |
| andFilterWhere() | Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}. |
Yiisoft\Db\Query\QueryPartsInterface |
| andHaving() | Adds HAVING condition to the existing one. | Yiisoft\Db\Query\QueryPartsInterface |
| andWhere() | Adds WHERE condition to the existing one. |
Yiisoft\Db\Query\QueryPartsInterface |
| distinct() | Sets the value indicating whether to SELECT DISTINCT or not. |
Yiisoft\Db\Query\QueryPartsInterface |
| filterHaving() | Sets the HAVING part of the query but ignores {@see Query::isEmpty()}. |
Yiisoft\Db\Query\QueryPartsInterface |
| filterWhere() | Sets the WHERE part of the query but ignores {@see Query::isEmpty()}. |
Yiisoft\Db\Query\QueryPartsInterface |
| for() | Sets the FOR part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| from() | Sets the FROM part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| groupBy() | Sets the GROUP BY part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| having() | Initially sets the HAVING part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| indexBy() | Sets the {@see indexBy} property. | Yiisoft\Db\Query\QueryPartsInterface |
| innerJoin() | Appends an INNER JOIN part to the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| join() | Appends a JOIN part to the query. | Yiisoft\Db\Query\QueryPartsInterface |
| leftJoin() | Appends a LEFT OUTER JOIN part to the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| limit() | Sets the LIMIT part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| offset() | Sets the OFFSET part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| orFilterHaving() | Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}. | Yiisoft\Db\Query\QueryPartsInterface |
| orFilterWhere() | Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}. |
Yiisoft\Db\Query\QueryPartsInterface |
| orHaving() | Adds HAVING condition to the existing one. |
Yiisoft\Db\Query\QueryPartsInterface |
| orWhere() | Adds WHERE condition to the existing one. |
Yiisoft\Db\Query\QueryPartsInterface |
| orderBy() | Sets the ORDER BY part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| rightJoin() | Appends a RIGHT OUTER JOIN part to the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| select() | Sets the SELECT part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| selectOption() | It allows you to specify more options for the SELECT clause of an SQL statement. |
Yiisoft\Db\Query\QueryPartsInterface |
| setFor() | Overwrites the FOR part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| setHaving() | Overwrites the HAVING part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| setJoins() | Specify the joins for a SELECT statement in a database query. |
Yiisoft\Db\Query\QueryPartsInterface |
| setUnions() | Specify queries for a SELECT statement that are combined with UNIONs. |
Yiisoft\Db\Query\QueryPartsInterface |
| setWhere() | Overwrites the WHERE part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| union() | Appends an SQL statement using UNION operator. |
Yiisoft\Db\Query\QueryPartsInterface |
| where() | Initially sets the WHERE part of the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| withQueries() | Specifies the WITH query clause for the query. |
Yiisoft\Db\Query\QueryPartsInterface |
| withQuery() | Set an SQL statement using WITH syntax. |
Yiisoft\Db\Query\QueryPartsInterface |
Method Details
| public abstract Yiisoft\Db\Query\QueryPartsInterface addFor ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
public function addFor(string|array|null $value): static;
Adds more group-by columns to the existing ones.
See also groupBy().
| public abstract Yiisoft\Db\Query\QueryPartsInterface addGroupBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
More columns to be grouped by. Columns can be specified in either a string (for example 'id, name') or an array (such as ['id', 'name']). The method will automatically quote the column names unless a column has some parenthesis (which means the column has 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 won't be able to correctly decide the group-by columns. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed to specify the |
public function addGroupBy(array|string|ExpressionInterface $columns): static;
Adds more ORDER BY columns to the query.
See also orderBy().
| public abstract Yiisoft\Db\Query\QueryPartsInterface addOrderBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns (and the directions) to be ordered by.
Columns can be specified in either a string (for example "id ASC, name DESC") or an array
(for example, |
public function addOrderBy(array|string|ExpressionInterface $columns): static;
Add more columns to the SELECT part of the query.
Note, that if {@see \Yiisoft\Db\Query\select} hasn't been specified before, you should include * explicitly if you want to select
all remaining columns too:
$query->addSelect(["*", "CONCAT(first_name, ' ', last_name) AS full_name"])->one();
See also select() for more details about the format of this parameter.
| public abstract Yiisoft\Db\Query\QueryPartsInterface addSelect ( array|Yiisoft\Db\Expression\ExpressionInterface|scalar $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|scalar |
The columns to add to the select. |
| Version | Description |
|---|---|
| 2.0.0 | `$columns` can be a scalar value or an array of scalar values. |
public function addSelect(array|bool|float|int|string|ExpressionInterface $columns): static;
Prepends an SQL statement using WITH syntax.
| public abstract Yiisoft\Db\Query\QueryPartsInterface addWithQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive = false ) | ||
| $query | Yiisoft\Db\Query\QueryInterface|string |
The SQL statement to append using |
| $alias | Yiisoft\Db\Expression\ExpressionInterface|string |
The query alias in |
| $recursive | boolean |
Its |
public function addWithQuery(
QueryInterface|string $query,
ExpressionInterface|string $alias,
bool $recursive = false,
): static;
Adds a filtering condition for a specific column and allow the user to choose a filter operator.
It adds WHERE condition for the given field and determines the comparison operator based on the first few
characters of the given value.
The condition is added in the same way as in {@see \Yiisoft\Db\Query\andFilterWhere()} so {@see \Yiisoft\Db\Query\Query::isEmpty()} are ignored.
The new condition and the existing one are joined using the AND operator.
The comparison operator is intelligently determined based on the first few characters in the given value.
In particular, it recognizes the following operators if they appear as the leading characters in the given value:
<: the column must be less than the given value.>: the column must be greater than the given value.<=: the column must be less than or equal to the given value.>=: the column must be greater than or equal to the given value.<>: the column must not be the same as the given value.=: the column must be equal to the given value.- If operator isn't regognized, the
$defaultOperatoris used.
| public abstract Yiisoft\Db\Query\QueryPartsInterface andFilterCompare ( string $column, string|null $value, string $defaultOperator = '=' ) | ||
| $column | string |
The column name. |
| $value | string|null |
The column value optionally prepended with the comparison operator. |
| $defaultOperator | string |
The operator to use when no operator is given in |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterCompare(string $column, ?string $value, string $defaultOperator = '='): static;
Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the AND operator.
This method is similar to {@see \Yiisoft\Db\Query\andHaving()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface andFilterHaving ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterHaving(array $condition): static;
Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the AND operator.
This method is similar to {@see \Yiisoft\Db\Query\andWhere()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface andFilterWhere ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function andFilterWhere(array $condition): static;
Adds HAVING condition to the existing one.
The new condition and the existing one will be joined using the AND operator.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface andHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new HAVING condition. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function andHaving(array|string|ExpressionInterface $condition, array $params = []): static;
Adds WHERE condition to the existing one.
The new condition and the existing one will be joined using the AND operator.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface andWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function andWhere(array|ExpressionInterface|string $condition, array $params = []): static;
Sets the value indicating whether to SELECT DISTINCT or not.
| public abstract Yiisoft\Db\Query\QueryPartsInterface distinct ( boolean $value = true ) | ||
| $value | boolean |
Whether to |
public function distinct(bool $value = true): static;
Sets the HAVING part of the query but ignores {@see Query::isEmpty()}.
This method is similar to {@see \Yiisoft\Db\Query\having()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
The following code shows the difference between this method and {@see \Yiisoft\Db\Query\having()}:
// HAVING `age`=:age
$query->filterHaving(['name' => null, 'age' => 20]);
// HAVING `age`=:age
$query->having(['age' => 20]);
// HAVING `name` IS NULL AND `age`=:age
$query->having(['name' => null, 'age' => 20]);
Note that unlike {@see \Yiisoft\Db\Query\having()}, you can't pass binding parameters to this method.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface filterHaving ( array $condition ) | ||
| $condition | array |
The conditions that should be in the |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function filterHaving(array $condition): static;
Sets the WHERE part of the query but ignores {@see Query::isEmpty()}.
This method is similar to {@see \Yiisoft\Db\Query\where()}.
The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}.
As a result, this method is best suited for building query conditions based on filter values entered by users.
The following code shows the difference between this method and {@see \Yiisoft\Db\Query\where()}:
// WHERE `age`=:age
$query->filterWhere(['name' => null, 'age' => 20]);
// WHERE `age`=:age
$query->where(['age' => 20]);
// WHERE `name` IS NULL AND `age`=:age
$query->where(['name' => null, 'age' => 20]);
Note that unlike {@see \Yiisoft\Db\Query\where()}, you can't pass binding parameters to this method.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface filterWhere ( array $condition ) | ||
| $condition | array |
The conditions that should be in the |
| throws | Yiisoft\Db\Exception\NotSupportedException |
If this query doesn't support filtering. |
|---|---|---|
public function filterWhere(array $condition): static;
| public abstract Yiisoft\Db\Query\QueryPartsInterface for ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
| throws | LogicException |
If |
|---|---|---|
public function for(string|array|null $value): static;
Sets the FROM part of the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface from ( array|Yiisoft\Db\Expression\ExpressionInterface|string $tables ) | ||
| $tables | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table(s) to select from.
This can be either a string (for example,
|
public function from(array|ExpressionInterface|string $tables): static;
Sets the GROUP BY part of the query.
See also addGroupBy().
| public abstract Yiisoft\Db\Query\QueryPartsInterface groupBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns to be grouped by. Columns can be specified in either a string (for example "id, name") or an array (such as ['id', 'name']). The method will automatically quote the column names unless a column has some parenthesis (which means the column has 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 won't be able to correctly decide the group-by columns. {@see \Yiisoft\Db\Expression\ExpressionInterface} object can be passed to specify the |
public function groupBy(array|string|ExpressionInterface $columns): static;
| public abstract Yiisoft\Db\Query\QueryPartsInterface having ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to be put after |
| $params | array |
The parameters (name => value) to bind to the query. |
| throws | LogicException |
If |
|---|---|---|
public function having(array|ExpressionInterface|string|null $condition, array $params = []): static;
Sets the {@see indexBy} property.
| public abstract Yiisoft\Db\Query\QueryPartsInterface indexBy ( Closure|string|null $column ) | ||
| $column | Closure|string|null |
The name of the column by which the query results should be indexed by. This can also be callable (for example, anonymous function) that returns the index value based on the given data. The signature of the callable should be:
|
public function indexBy(string|Closure|null $column): static;
Appends an INNER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface innerJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
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 (such as 'public.user') and/or table alias (such as 'user u'). The method will automatically quote the table name unless it has 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 {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function innerJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Appends a JOIN part to the query.
The first parameter specifies what type of join it is.
| public abstract Yiisoft\Db\Query\QueryPartsInterface join ( string $type, array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $type | string |
The type of join, such as |
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to join. Use a string to represent the name of the table to join. The table name can contain a schema prefix (such as 'public.user') and/or table alias (e.g. 'user u'). The method will automatically quote the table name unless it has 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 {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\where()} on how to specify this parameter. Keys and values of an associative array are treated as column names and will be quoted before being used in an SQL query. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function join(
string $type,
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Appends a LEFT OUTER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface leftJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The table to join. Use a string to represent the name of the table to join. The table name can contain a schema prefix (such as 'public.user') and/or table alias (such as 'user u'). The method will automatically quote the table name unless it has 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 {@see \Yiisoft\Db\Query\Query} object representing the sub-query while the corresponding key represents the alias for the sub-query. |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to bind to the query. |
public function leftJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Sets the LIMIT part of the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface limit ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $limit ) | ||
| $limit | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
The limit. Use null or negative value to disable limit. |
public function limit(ExpressionInterface|int|null $limit): static;
Sets the OFFSET part of the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface offset ( Yiisoft\Db\Expression\ExpressionInterface|integer|null $offset ) | ||
| $offset | Yiisoft\Db\Expression\ExpressionInterface|integer|null |
$offset The offset. Use |
public function offset(ExpressionInterface|int|null $offset): static;
Adds HAVING condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the OR operator.
This method is similar to {@see \Yiisoft\Db\Query\orHaving()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface orFilterHaving ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function orFilterHaving(array $condition): static;
Adds WHERE condition to the existing one but ignores {@see Query::isEmpty()}.
The new condition and the existing one will be joined using the 'OR' operator.
This method is similar to {@see \Yiisoft\Db\Query\orWhere()}. The main difference is that this method will remove {@see \Yiisoft\Db\Query\Query::isEmpty()}. As a result, this method is best suited for building query conditions based on filter values entered by users.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface orFilterWhere ( array $condition ) | ||
| $condition | array |
The new |
| throws | Yiisoft\Db\Exception\NotSupportedException | |
|---|---|---|
public function orFilterWhere(array $condition): static;
Adds HAVING condition to the existing one.
The new condition and the existing one will be joined using the OR operator.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface orHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to bind to the query. |
public function orHaving(array|string|ExpressionInterface $condition, array $params = []): static;
Adds WHERE condition to the existing one.
The new condition and the existing one will be joined using the OR operator.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface orWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The new |
| $params | array |
The parameters (name => value) to bind to the query. |
public function orWhere(array|string|ExpressionInterface $condition, array $params = []): static;
Sets the ORDER BY part of the query.
See also addOrderBy().
| public abstract Yiisoft\Db\Query\QueryPartsInterface orderBy ( array|Yiisoft\Db\Expression\ExpressionInterface|string $columns ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The columns (and the directions) to be ordered by.
Columns can be specified in either a string (for example |
public function orderBy(array|string|ExpressionInterface $columns): static;
Appends a RIGHT OUTER JOIN part to the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface rightJoin ( array|Yiisoft\Db\Expression\ExpressionInterface|string $table, array|Yiisoft\Db\Expression\ExpressionInterface|string $on = '', array $params = [] ) | ||
| $table | array|Yiisoft\Db\Expression\ExpressionInterface|string |
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 (such as |
| $on | array|Yiisoft\Db\Expression\ExpressionInterface|string |
The join condition that should appear in the ON part. Please refer to {@see \Yiisoft\Db\Query\join()} on how to specify this parameter. |
| $params | array |
The parameters (name => value) to be bound to the query. |
public function rightJoin(
array|ExpressionInterface|string $table,
array|ExpressionInterface|string $on = '',
array $params = [],
): static;
Sets the SELECT part of the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface select ( array|Yiisoft\Db\Expression\ExpressionInterface|scalar $columns, string|null $option = null ) | ||
| $columns | array|Yiisoft\Db\Expression\ExpressionInterface|scalar |
The columns to be selected.
Columns can be specified in either a string (for example |
| $option | string|null |
More option that should be appended to the 'SELECT' keyword. For example, in MySQL, the option 'SQL_CALC_FOUND_ROWS' can be used. |
| Version | Description |
|---|---|
| 2.0.0 | `$columns` can be a scalar value or an array of scalar values. For example, `$query->select(1)` will be converted to `SELECT 1`. |
public function select(array|bool|float|int|string|ExpressionInterface $columns, ?string $option = null): static;
It allows you to specify more options for the SELECT clause of an SQL statement.
| public abstract Yiisoft\Db\Query\QueryPartsInterface selectOption ( string|null $value ) | ||
| $value | string|null |
More option that should be appended to the 'SELECT' keyword.
For example, in MySQL, the option |
public function selectOption(?string $value): static;
| public abstract Yiisoft\Db\Query\QueryPartsInterface setFor ( array|string|null $value ) | ||
| $value | array|string|null |
The value(s) to be set for the |
public function setFor(string|array|null $value): static;
Overwrites the HAVING part of the query.
See also having().
| public abstract Yiisoft\Db\Query\QueryPartsInterface setHaving ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to be put after |
| $params | array |
The parameters (name => value) to bind to the query. |
public function setHaving(array|ExpressionInterface|string|null $condition, array $params = []): static;
Specify the joins for a SELECT statement in a database query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface setJoins ( array $value ) | ||
| $value | array |
The joins to perform in the query. The format is the following:
|
public function setJoins(array $value): static;
Specify queries for a SELECT statement that are combined with UNIONs.
| public abstract Yiisoft\Db\Query\QueryPartsInterface setUnions ( array $value ) | ||
| $value | array |
The queries to union such as |
public function setUnions(array $value): static;
Overwrites the WHERE part of the query.
See also where().
| public abstract Yiisoft\Db\Query\QueryPartsInterface setWhere ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to put in the |
| $params | array |
The parameters (name => value) to bind to the query. |
public function setWhere(array|string|ExpressionInterface|null $condition, array $params = []): static;
Appends an SQL statement using UNION operator.
| public abstract Yiisoft\Db\Query\QueryPartsInterface union ( Yiisoft\Db\Query\QueryInterface|string $sql, boolean $all = false ) | ||
| $sql | Yiisoft\Db\Query\QueryInterface|string |
$sql The SQL statement to be appended using |
| $all | boolean |
|
public function union(QueryInterface|string $sql, bool $all = false): static;
Initially sets the WHERE part of the query.
The $condition specified as an array can be in one of the following two formats:
- key-value format:
['column1' => value1, 'column2' => value2, ...] - operator format:
[operator, operand1, operand2, ...]
A condition in key-value format represents the following SQL expression in general:
column1=value1 AND column2=value2 AND .... In case when a value is an array,
an IN expression will be generated. And if a value is null, IS NULL will be used in the generated
expression. Below are some examples:
['type' => 1, 'status' => 2]generates(type = 1) AND (status = 2).['id' => [1, 2, 3], 'status' => 2]generates(id IN (1, 2, 3)) AND (status = 2).['status' => null]generatesstatus IS NULL.
A condition in operator format generates the SQL expression according to the specified operator, which can be one of the following:
and: the operands should be concatenated together using
AND. For example,['and', 'id=1', 'id=2']will generateid=1 AND id=2. If an operand is an array, it will be converted into a string using the rules described here. For example,['and', 'type=1', ['or', 'id=1', 'id=2']]will generatetype=1 AND (id=1 OR id=2). The method will not do any quoting or escaping.or: similar to the
andoperator except that the operands are concatenated usingOR. For example,['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]]]will generate(type IN (7, 8, 9) OR (id IN (1, 2, 3))).not: this will take only one operand and build the negation of it by prefixing the query string with
NOT. For example['not', ['attribute' => null]]will result in the conditionNOT (attribute IS NULL).between: operand 1 should be the column name, and operand 2 and 3 should be the starting and ending values of the range that the column is in. For example,
['between', 'id', 1, 10]will generateid BETWEEN 1 AND 10.not between: similar to
betweenexcept theBETWEENis replaced withNOT BETWEENin the generated condition.in: operand 1 should be a column or DB expression, and operand 2 be an array representing the range of the values that the column or DB expression should be in. For example,
['in', 'id', [1, 2, 3]]will generateid IN (1, 2, 3). The method will quote the column name and escape values in the range.To create a composite
INcondition you can use and array for the column name and value, where the values are indexed by the column name:['in', ['id', 'name'], [['id' => 1, 'name' => 'foo'], ['id' => 2, 'name' => 'bar']] ].You may also specify a sub-query that's used to get the values for the
IN-condition:['in', 'user_id', (new Query())->select('id')->from('users')->where(['active' => 1])]not in: similar to the
inoperator except thatINis replaced withNOT INin the generated condition.like: operand 1 should be a column or DB expression, and operand 2 be a string or an array representing the values that the column or DB expression should be like. For example,
['like', 'name', 'tester']will generatename LIKE '%tester%'. When the value range is given as an array, manyLIKEpredicates will be generated and concatenated usingAND. For example,['like', 'name', ['test', 'sample']]will generatename LIKE '%test%' AND name LIKE '%sample%'. The method will quote the column name and escape special characters in the values. Sometimes, you may want to add the percentage characters to the matching value by yourself. You may supply a third operandfalseto do so. For example,['like', 'name', '%tester', false]will generatename LIKE '%tester'.or like: similar to the
likeoperator except thatORis used to concatenate theLIKEpredicates when operand 2 is an array.not like: similar to the
likeoperator except thatLIKEis replaced withNOT LIKEin the generated condition.or not like: similar to the
not likeoperator except thatORis used to concatenate theNOT LIKEpredicates.exists: operand 1 is a query object that used to build an
EXISTScondition. For example['exists', (new Query())->select('id')->from('users')->where(['active' => 1])]will result in the following SQL expression:EXISTS (SELECT "id" FROM "users" WHERE "active"=1).not exists: similar to the
existsoperator except thatEXISTSis replaced withNOT EXISTSin the generated condition.Additionally, you can specify arbitrary operators as follows: A condition of
['>=', 'id', 10]will result in the following SQL expression:id >= 10.
Note that this method will override any existing WHERE condition. You might want to use {@see \Yiisoft\Db\Query\andWhere()}
or {@see \Yiisoft\Db\Query\orWhere()} instead.
See also:
| public abstract Yiisoft\Db\Query\QueryPartsInterface where ( array|Yiisoft\Db\Expression\ExpressionInterface|string|null $condition, array $params = [] ) | ||
| $condition | array|Yiisoft\Db\Expression\ExpressionInterface|string|null |
The conditions to put in the |
| $params | array |
The parameters (name => value) to bind to the query. |
| throws | LogicException |
If |
|---|---|---|
public function where(array|string|ExpressionInterface|null $condition, array $params = []): static;
Specifies the WITH query clause for the query.
| public abstract Yiisoft\Db\Query\QueryPartsInterface withQueries ( Yiisoft\Db\Query\WithQuery $queries ) | ||
| $queries | Yiisoft\Db\Query\WithQuery |
The |
public function withQueries(WithQuery ...$queries): static;
Set an SQL statement using WITH syntax.
| public abstract Yiisoft\Db\Query\QueryPartsInterface withQuery ( Yiisoft\Db\Query\QueryInterface|string $query, Yiisoft\Db\Expression\ExpressionInterface|string $alias, boolean $recursive = false ) | ||
| $query | Yiisoft\Db\Query\QueryInterface|string |
The SQL statement. |
| $alias | Yiisoft\Db\Expression\ExpressionInterface|string |
The query alias in |
| $recursive | boolean |
Its |
public function withQuery(
QueryInterface|string $query,
ExpressionInterface|string $alias,
bool $recursive = false,
): static;
Signup or Login in order to comment.