# QueryBuilder

> QueryBuilder is the query builder for MySQL databases (MySQL 8.0+ and MariaDB 10.6+).

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [db](/docs/api/yii/db/index.md) › [mysql](/docs/api/yii/db/mysql/index.md) › QueryBuilder

| class | `yii\db\mysql\QueryBuilder` |
| --- | --- |
| Extends | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) » [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| Implements | [`yii\base\Configurable`](/docs/api/yii/base/configurable/index.md) |
| Since | 2.0 |
| Source | [`framework/db/mysql/QueryBuilder.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/db/mysql/QueryBuilder.php#L35) |

QueryBuilder is the query builder for MySQL databases (MySQL 8.0+ and MariaDB 10.6+).

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-db"></span>[`$db`](/docs/api/yii/db/querybuilder/index.md#property-db) | [`yii\db\Connection`](/docs/api/yii/db/connection/index.md) | The database connection. Default: `null`. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="property-separator"></span>[`$separator`](/docs/api/yii/db/querybuilder/index.md#property-separator) | `string` | The separator between different fragments of a SQL statement. Default: `' '`. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="property-typeMap"></span>`$typeMap` | `array` | Mapping from abstract column types (keys) to physical column types (values). Default: `[\yii\db\mysql\Schema::TYPE_PK => 'int NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_UPK => 'int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_BIGPK => 'bigint NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_UBIGPK => 'bigint UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', \yii\db\mysql\Schema::TYPE_CHAR => 'char(1)', \yii\db\mysql\Schema::TYPE_STRING => 'varchar(255)', \yii\db\mysql\Schema::TYPE_TEXT => 'text', \yii\db\mysql\Schema::TYPE_TINYINT => 'tinyint', \yii\db\mysql\Schema::TYPE_SMALLINT => 'smallint', \yii\db\mysql\Schema::TYPE_INTEGER => 'int', \yii\db\mysql\Schema::TYPE_BIGINT => 'bigint', \yii\db\mysql\Schema::TYPE_FLOAT => 'float', \yii\db\mysql\Schema::TYPE_DOUBLE => 'double', \yii\db\mysql\Schema::TYPE_DECIMAL => 'decimal(10,0)', \yii\db\mysql\Schema::TYPE_DATE => 'date', \yii\db\mysql\Schema::TYPE_BINARY => 'blob', \yii\db\mysql\Schema::TYPE_BOOLEAN => 'tinyint(1)', \yii\db\mysql\Schema::TYPE_MONEY => 'decimal(19,4)', \yii\db\mysql\Schema::TYPE_JSON => 'json']`. | `QueryBuilder` |
| <span id="property-conditionClasses"></span>[`$conditionClasses`](/docs/api/yii/db/querybuilder/index.md#property-conditionClasses) protected | `array` | Map of condition aliases to condition classes. Default: `[]`. Available since 2.0.14. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="property-expressionBuilders"></span>[`$expressionBuilders`](/docs/api/yii/db/querybuilder/index.md#property-expressionBuilders) protected | `string[]\|`[`yii\db\ExpressionBuilderInterface`](/docs/api/yii/db/expressionbuilderinterface/index.md)`[]` | Maps expression class to expression builder class. Default: `[]`. Available since 2.0.14. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| <span id="method-__construct"></span>[`__construct()`](/docs/api/yii/db/querybuilder/index.md#method-__construct) | Constructor. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-__call"></span>[`__call()`](/docs/api/yii/base/baseobject/index.md#method-__call) | Calls the named method which is not a class method. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__get"></span>[`__get()`](/docs/api/yii/base/baseobject/index.md#method-__get) | Returns the value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__isset"></span>[`__isset()`](/docs/api/yii/base/baseobject/index.md#method-__isset) | Checks if a property is set, i.e. defined and not null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__set"></span>[`__set()`](/docs/api/yii/base/baseobject/index.md#method-__set) | Sets value of an object property. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-__unset"></span>[`__unset()`](/docs/api/yii/base/baseobject/index.md#method-__unset) | Sets an object property to null. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-addCheck"></span>[`addCheck()`](/docs/api/yii/db/querybuilder/index.md#method-addCheck) | Creates a SQL command for adding a check constraint to an existing table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-addColumn"></span>[`addColumn()`](/docs/api/yii/db/querybuilder/index.md#method-addColumn) | Builds a SQL statement for adding a new DB column. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`addCommentOnColumn()`](#method-addCommentOnColumn) | Builds a SQL command for adding comment to column. | `QueryBuilder` |
| [`addCommentOnTable()`](#method-addCommentOnTable) | Builds a SQL command for adding comment to table. | `QueryBuilder` |
| <span id="method-addDefaultValue"></span>[`addDefaultValue()`](/docs/api/yii/db/querybuilder/index.md#method-addDefaultValue) | Creates a SQL command for adding a default value constraint to an existing table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-addForeignKey"></span>[`addForeignKey()`](/docs/api/yii/db/querybuilder/index.md#method-addForeignKey) | Builds a SQL statement for adding a foreign key constraint to an existing table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-addPrimaryKey"></span>[`addPrimaryKey()`](/docs/api/yii/db/querybuilder/index.md#method-addPrimaryKey) | Builds a SQL statement for adding a primary key constraint to an existing table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-addUnique"></span>[`addUnique()`](/docs/api/yii/db/querybuilder/index.md#method-addUnique) | Creates a SQL command for adding an unique constraint to an existing table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-alterColumn"></span>[`alterColumn()`](/docs/api/yii/db/querybuilder/index.md#method-alterColumn) | Builds a SQL statement for changing the definition of a column. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-batchInsert"></span>[`batchInsert()`](/docs/api/yii/db/querybuilder/index.md#method-batchInsert) | Generates a batch INSERT SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-bindParam"></span>[`bindParam()`](/docs/api/yii/db/querybuilder/index.md#method-bindParam) | Helper method to add $value to $params array using [`PARAM_PREFIX`](/docs/api/yii/db/querybuilder/index.md#const-PARAM_PREFIX). | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-build"></span>[`build()`](/docs/api/yii/db/querybuilder/index.md#method-build) | Generates a SELECT SQL statement from a [`Query`](/docs/api/yii/db/query/index.md) object. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildColumns"></span>[`buildColumns()`](/docs/api/yii/db/querybuilder/index.md#method-buildColumns) | Processes columns and properly quotes them if necessary. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildCondition"></span>[`buildCondition()`](/docs/api/yii/db/querybuilder/index.md#method-buildCondition) | Parses the condition specification and generates the corresponding SQL expression. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildExpression"></span>[`buildExpression()`](/docs/api/yii/db/querybuilder/index.md#method-buildExpression) | Builds given $expression | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildFrom"></span>[`buildFrom()`](/docs/api/yii/db/querybuilder/index.md#method-buildFrom) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildGroupBy"></span>[`buildGroupBy()`](/docs/api/yii/db/querybuilder/index.md#method-buildGroupBy) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildHaving"></span>[`buildHaving()`](/docs/api/yii/db/querybuilder/index.md#method-buildHaving) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildJoin"></span>[`buildJoin()`](/docs/api/yii/db/querybuilder/index.md#method-buildJoin) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`buildLimit()`](#method-buildLimit) |  | `QueryBuilder` |
| <span id="method-buildOrderBy"></span>[`buildOrderBy()`](/docs/api/yii/db/querybuilder/index.md#method-buildOrderBy) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildOrderByAndLimit"></span>[`buildOrderByAndLimit()`](/docs/api/yii/db/querybuilder/index.md#method-buildOrderByAndLimit) | Builds the ORDER BY and LIMIT/OFFSET clauses and appends them to the given SQL. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildSelect"></span>[`buildSelect()`](/docs/api/yii/db/querybuilder/index.md#method-buildSelect) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildUnion"></span>[`buildUnion()`](/docs/api/yii/db/querybuilder/index.md#method-buildUnion) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildUnionOrderByAndLimit"></span>[`buildUnionOrderByAndLimit()`](/docs/api/yii/db/querybuilder/index.md#method-buildUnionOrderByAndLimit) | Builds the global ORDER BY and LIMIT/OFFSET clauses for a UNION query. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildWhere"></span>[`buildWhere()`](/docs/api/yii/db/querybuilder/index.md#method-buildWhere) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-buildWithQueries"></span>[`buildWithQueries()`](/docs/api/yii/db/querybuilder/index.md#method-buildWithQueries) |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-canGetProperty"></span>[`canGetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canGetProperty) | Returns a value indicating whether a property can be read. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-canSetProperty"></span>[`canSetProperty()`](/docs/api/yii/base/baseobject/index.md#method-canSetProperty) | Returns a value indicating whether a property can be set. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`checkIntegrity()`](#method-checkIntegrity) | Builds a SQL statement for enabling or disabling integrity check. | `QueryBuilder` |
| <span id="method-className"></span>[`className()`](/docs/api/yii/base/baseobject/index.md#method-className) static | Returns the fully qualified name of this class. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-createConditionFromArray"></span>[`createConditionFromArray()`](/docs/api/yii/db/querybuilder/index.md#method-createConditionFromArray) | Transforms $condition defined in array format (as described in [`Query::where()`](/docs/api/yii/db/query/index.md#method-where) to instance of ConditionInterface according to [`$conditionClasses`](/docs/api/yii/db/querybuilder/index.md#property-conditionClasses) map. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`createIndex()`](#method-createIndex) | Builds a SQL statement for creating a new index. | `QueryBuilder` |
| <span id="method-createTable"></span>[`createTable()`](/docs/api/yii/db/querybuilder/index.md#method-createTable) | Builds a SQL statement for creating a new DB table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-createView"></span>[`createView()`](/docs/api/yii/db/querybuilder/index.md#method-createView) | Creates a SQL View. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-delete"></span>[`delete()`](/docs/api/yii/db/querybuilder/index.md#method-delete) | Creates a DELETE SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-dropCheck"></span>[`dropCheck()`](/docs/api/yii/db/querybuilder/index.md#method-dropCheck) | Creates a SQL command for dropping a check constraint. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-dropColumn"></span>[`dropColumn()`](/docs/api/yii/db/querybuilder/index.md#method-dropColumn) | Builds a SQL statement for dropping a DB column. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`dropCommentFromColumn()`](#method-dropCommentFromColumn) | Builds a SQL command for adding comment to column. | `QueryBuilder` |
| [`dropCommentFromTable()`](#method-dropCommentFromTable) | Builds a SQL command for adding comment to table. | `QueryBuilder` |
| <span id="method-dropDefaultValue"></span>[`dropDefaultValue()`](/docs/api/yii/db/querybuilder/index.md#method-dropDefaultValue) | Creates a SQL command for dropping a default value constraint. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`dropForeignKey()`](#method-dropForeignKey) | Builds a SQL statement for dropping a foreign key constraint. | `QueryBuilder` |
| <span id="method-dropIndex"></span>[`dropIndex()`](/docs/api/yii/db/querybuilder/index.md#method-dropIndex) | Builds a SQL statement for dropping an index. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`dropPrimaryKey()`](#method-dropPrimaryKey) | Builds a SQL statement for removing a primary key constraint from an existing table. | `QueryBuilder` |
| <span id="method-dropTable"></span>[`dropTable()`](/docs/api/yii/db/querybuilder/index.md#method-dropTable) | Builds a SQL statement for dropping a DB table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`dropUnique()`](#method-dropUnique) | Creates a SQL command for dropping an unique constraint. | `QueryBuilder` |
| <span id="method-dropView"></span>[`dropView()`](/docs/api/yii/db/querybuilder/index.md#method-dropView) | Drops a SQL View. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-executeResetSequence"></span>[`executeResetSequence()`](/docs/api/yii/db/querybuilder/index.md#method-executeResetSequence) | Execute a SQL statement for resetting the sequence value of a table's primary key. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-getColumnType"></span>[`getColumnType()`](/docs/api/yii/db/querybuilder/index.md#method-getColumnType) | Converts an abstract column type into a physical column type. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-getExpressionBuilder"></span>[`getExpressionBuilder()`](/docs/api/yii/db/querybuilder/index.md#method-getExpressionBuilder) | Gets object of [`ExpressionBuilderInterface`](/docs/api/yii/db/expressionbuilderinterface/index.md) that is suitable for $expression. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-hasMethod"></span>[`hasMethod()`](/docs/api/yii/base/baseobject/index.md#method-hasMethod) | Returns a value indicating whether a method is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| <span id="method-hasProperty"></span>[`hasProperty()`](/docs/api/yii/base/baseobject/index.md#method-hasProperty) | Returns a value indicating whether a property is defined. | [`yii\base\BaseObject`](/docs/api/yii/base/baseobject/index.md) |
| [`init()`](#method-init) | Initializes the object. | `QueryBuilder` |
| <span id="method-insert"></span>[`insert()`](/docs/api/yii/db/querybuilder/index.md#method-insert) | Creates an INSERT SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`isMariaDb()`](#method-isMariaDb) | Returns whether the connected server is MariaDB. | `QueryBuilder` |
| <span id="method-renameColumn"></span>[`renameColumn()`](/docs/api/yii/db/querybuilder/index.md#method-renameColumn) | Builds a SQL statement for renaming a column. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-renameTable"></span>[`renameTable()`](/docs/api/yii/db/querybuilder/index.md#method-renameTable) | Builds a SQL statement for renaming a DB table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`resetSequence()`](#method-resetSequence) | Creates a SQL statement for resetting the sequence value of a table's primary key. | `QueryBuilder` |
| [`selectExists()`](#method-selectExists) | Creates a SELECT EXISTS() SQL statement. | `QueryBuilder` |
| <span id="method-setConditionClasses"></span>[`setConditionClasses()`](/docs/api/yii/db/querybuilder/index.md#method-setConditionClasses) | Setter for [`$conditionClasses`](/docs/api/yii/db/querybuilder/index.md#property-conditionClasses) property. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-setExpressionBuilders"></span>[`setExpressionBuilders()`](/docs/api/yii/db/querybuilder/index.md#method-setExpressionBuilders) | Setter for [`$expressionBuilders`](/docs/api/yii/db/querybuilder/index.md#property-expressionBuilders) property. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-truncateTable"></span>[`truncateTable()`](/docs/api/yii/db/querybuilder/index.md#method-truncateTable) | Builds a SQL statement for truncating a DB table. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-update"></span>[`update()`](/docs/api/yii/db/querybuilder/index.md#method-update) | Creates an UPDATE SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`upsert()`](#method-upsert) | Creates an SQL statement to insert rows into a database table if they do not already exist (matching unique constraints), or update them if they do. | `QueryBuilder` |
| [`buildOffsetFetch()`](#method-buildOffsetFetch) protected | Builds the OFFSET/FETCH clauses for MariaDB 10.6 or newer. | `QueryBuilder` |
| <span id="method-concatUnionSql"></span>[`concatUnionSql()`](/docs/api/yii/db/querybuilder/index.md#method-concatUnionSql) protected | Concatenates the first `SELECT` statement with the UNION clauses built by [`buildUnion()`](/docs/api/yii/db/querybuilder/index.md#method-buildUnion). | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-defaultConditionClasses"></span>[`defaultConditionClasses()`](/docs/api/yii/db/querybuilder/index.md#method-defaultConditionClasses) protected | Contains array of default condition classes. Extend this method, if you want to change default condition classes for the query builder. See [`$conditionClasses`](/docs/api/yii/db/querybuilder/index.md#property-conditionClasses) docs for details. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`defaultExpressionBuilders()`](#method-defaultExpressionBuilders) protected | Contains array of default expression builders. Extend this method and override it, if you want to change default expression builders for this query builder. See [`$expressionBuilders`](/docs/api/yii/db/querybuilder/index.md#property-expressionBuilders) docs for details. | `QueryBuilder` |
| <span id="method-extractAlias"></span>[`extractAlias()`](/docs/api/yii/db/querybuilder/index.md#method-extractAlias) protected | Extracts table alias if there is one or returns false | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`hasLimit()`](#method-hasLimit) protected | Checks to see if the given limit is effective. | `QueryBuilder` |
| [`hasOffset()`](#method-hasOffset) protected | Checks to see if the given offset is effective. | `QueryBuilder` |
| <span id="method-prepareInsertSelectSubQuery"></span>[`prepareInsertSelectSubQuery()`](/docs/api/yii/db/querybuilder/index.md#method-prepareInsertSelectSubQuery) protected | Prepare select-subquery and field names for INSERT INTO ... SELECT SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| [`prepareInsertValues()`](#method-prepareInsertValues) protected | Prepares a `VALUES` part for an `INSERT` SQL statement. | `QueryBuilder` |
| <span id="method-prepareSelectQuery"></span>[`prepareSelectQuery()`](/docs/api/yii/db/querybuilder/index.md#method-prepareSelectQuery) protected | Performs driver-specific preparation after [`Query::prepare()`](/docs/api/yii/db/query/index.md#method-prepare). | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-prepareUpdateSets"></span>[`prepareUpdateSets()`](/docs/api/yii/db/querybuilder/index.md#method-prepareUpdateSets) protected | Prepares a `SET` parts for an `UPDATE` SQL statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-prepareUpsertColumns"></span>[`prepareUpsertColumns()`](/docs/api/yii/db/querybuilder/index.md#method-prepareUpsertColumns) protected |  | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |
| <span id="method-prepareUpsertSets"></span>[`prepareUpsertSets()`](/docs/api/yii/db/querybuilder/index.md#method-prepareUpsertSets) protected | Prepares the `SET` assignments for the update branch of an upsert statement. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |

## Constants

| Constant | Value | Description | Defined by |
| --- | --- | --- | --- |
| <span id="const-PARAM_PREFIX"></span>[`PARAM_PREFIX`](/docs/api/yii/db/querybuilder/index.md#const-PARAM_PREFIX) | `':qp'` | The prefix for automatically generated query binding parameters. | [`yii\db\QueryBuilder`](/docs/api/yii/db/querybuilder/index.md) |

## Method details

### addCommentOnColumn() <span id="method-addCommentOnColumn"></span>

```php
public function addCommentOnColumn(mixed $table, mixed $column, mixed $comment): string
```

Builds a SQL command for adding comment to column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table whose column is to be commented. The table name will be properly quoted by the method. |
| `$column` | `mixed` | The name of the column to be commented. The column name will be properly quoted by the method. |
| `$comment` | `mixed` | The text of the comment to be added. The comment will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for adding comment on column

**Since**

2.0.8

### addCommentOnTable() <span id="method-addCommentOnTable"></span>

```php
public function addCommentOnTable(mixed $table, mixed $comment): string
```

Builds a SQL command for adding comment to table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table whose column is to be commented. The table name will be properly quoted by the method. |
| `$comment` | `mixed` | The text of the comment to be added. The comment will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for adding comment on table

**Since**

2.0.8

### buildLimit() <span id="method-buildLimit"></span>

```php
public function buildLimit(mixed $limit, mixed $offset): string
```

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$limit` | `mixed` |  |
| `$offset` | `mixed` |  |

**Returns**

`string`: The LIMIT and OFFSET clauses

### checkIntegrity() <span id="method-checkIntegrity"></span>

```php
public function checkIntegrity(bool $check = true, string $schema = '', string $table = ''): string
```

Builds a SQL statement for enabling or disabling integrity check.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$check` | `bool` | Whether to turn on or off the integrity check. |
| `$schema` | `string` | The schema of the tables. Meaningless for MySQL. |
| `$table` | `string` | The table name. Meaningless for MySQL. |

**Returns**

`string`: The SQL statement for checking integrity

### createIndex() <span id="method-createIndex"></span>

```php
public function createIndex(string $name, string $table, array|string $columns, bool $unique = false): string
```

Builds a SQL statement for creating a new index.

MySQL emits `ALTER TABLE ... ADD [UNIQUE] INDEX` instead of `CREATE INDEX`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the index. The name will be properly quoted by the method. |
| `$table` | `string` | The table that the new index will be created for. The name will be properly quoted by the method. |
| `$columns` | `array\|string` | The column(s) that should be included in the index. If there are multiple columns, separate them with commas or use an array to represent them. Each column name will be properly quoted by the method, unless a parenthesis is found in the name. |
| `$unique` | `bool` | Whether to add a `UNIQUE` constraint on the created index. |

**Returns**

`string`: The SQL statement for creating a new index.

**See also**

- <https://bugs.mysql.com/bug.php?id=48875>

### dropCommentFromColumn() <span id="method-dropCommentFromColumn"></span>

```php
public function dropCommentFromColumn(mixed $table, mixed $column): string
```

Builds a SQL command for adding comment to column.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table whose column is to be commented. The table name will be properly quoted by the method. |
| `$column` | `mixed` | The name of the column to be commented. The column name will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for adding comment on column

**Since**

2.0.8

### dropCommentFromTable() <span id="method-dropCommentFromTable"></span>

```php
public function dropCommentFromTable(mixed $table): string
```

Builds a SQL command for adding comment to table.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table whose column is to be commented. The table name will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for adding comment on column

**Since**

2.0.8

### dropForeignKey() <span id="method-dropForeignKey"></span>

```php
public function dropForeignKey(string $name, string $table): string
```

Builds a SQL statement for dropping a foreign key constraint.

MySQL emits `ALTER TABLE ... DROP FOREIGN KEY` instead of the standard `DROP CONSTRAINT`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the foreign key constraint to be dropped. The name will be properly quoted by the method. |
| `$table` | `string` | The table whose foreign key is to be dropped. The name will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for dropping a foreign key constraint.

### dropPrimaryKey() <span id="method-dropPrimaryKey"></span>

```php
public function dropPrimaryKey(string $name, string $table): string
```

Builds a SQL statement for removing a primary key constraint from an existing table.

MySQL emits `ALTER TABLE ... DROP PRIMARY KEY` and ignores the constraint name, since a MySQL primary key is
always named `PRIMARY`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | The name of the primary key constraint to be removed. Ignored by MySQL. |
| `$table` | `string` | The table that the primary key constraint will be removed from. The name will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for removing a primary key constraint from an existing table.

### dropUnique() <span id="method-dropUnique"></span>

```php
public function dropUnique(mixed $name, mixed $table): string
```

Creates a SQL command for dropping an unique constraint.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `mixed` | The name of the unique constraint to be dropped. The name will be properly quoted by the method. |
| `$table` | `mixed` | The table whose unique constraint is to be dropped. The name will be properly quoted by the method. |

**Returns**

`string`: The SQL statement for dropping an unique constraint.

**Since**

2.0.13

### init() <span id="method-init"></span>

```php
public function init(): mixed
```

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the
given configuration.

**Returns**

`mixed`

### isMariaDb() <span id="method-isMariaDb"></span>

```php
public function isMariaDb(): bool
```

Returns whether the connected server is MariaDB.

**Returns**

`bool`: Whether the connected server is MariaDB.

### resetSequence() <span id="method-resetSequence"></span>

```php
public function resetSequence(string $tableName, int|null $value = null): string
```

Creates a SQL statement for resetting the sequence value of a table's primary key.

The sequence will be reset such that the primary key of the next new row inserted will have the specified value
or the maximum existing value `+1`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$tableName` | `string` | The name of the table whose primary key sequence will be reset. |
| `$value` | `int\|null` | The integer value for the primary key of the next new row inserted. If this is not set, the next new row's primary key will have the maximum existing value `+1`. |

**Returns**

`string`: The SQL statement for resetting sequence.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if the table does not exist or there is no sequence associated with the table.

### selectExists() <span id="method-selectExists"></span>

```php
public function selectExists(mixed $rawSql): string
```

Creates a SELECT EXISTS() SQL statement.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$rawSql` | `mixed` | The subquery in a raw form to select from. |

**Returns**

`string`: The SELECT EXISTS() SQL statement.

**Since**

2.0.8

### upsert() <span id="method-upsert"></span>

```php
public function upsert(mixed $table, mixed $insertColumns, mixed $updateColumns, mixed &$params): string
```

Creates an SQL statement to insert rows into a database table if
they do not already exist (matching unique constraints),
or update them if they do.

For example,

```
$sql = $queryBuilder->upsert('pages', [
    'name' => 'Front page',
    'url' => 'https://example.com/', // url is unique
    'visits' => 0,
], [
    'visits' => new \yii\db\Expression('visits + 1'),
], $params);
```

The method will properly escape the table and column names.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table that new rows will be inserted into/updated in. |
| `$insertColumns` | `mixed` | The column data (name => value) to be inserted into the table or instance of [`Query`](/docs/api/yii/db/query/index.md) to perform `INSERT INTO ... SELECT` SQL statement. |
| `$updateColumns` | `mixed` | The column data (name => value) to be updated if they already exist. If `true` is passed, the column data will be updated to match the insert column data. If `false` is passed, no update will be performed if the column data already exists. |
| `$params` | `mixed` | The binding parameters that will be generated by this method. They should be bound to the DB command later. |

**Returns**

`string`: The resulting SQL.

**Throws**

- [`yii\base\NotSupportedException`](/docs/api/yii/base/notsupportedexception/index.md): if this is not supported by the underlying DBMS.

**Since**

2.0.14

**See also**

- <https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html>

### buildOffsetFetch() <span id="method-buildOffsetFetch"></span>

```php
protected function buildOffsetFetch(int|yii\db\Expression $limit, int|yii\db\Expression $offset): string
```

Builds the OFFSET/FETCH clauses for MariaDB 10.6 or newer.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$limit` | `int\|`[`yii\db\Expression`](/docs/api/yii/db/expression/index.md) | The limit number. See [`Query::limit()`](/docs/api/yii/db/querytrait/index.md#method-limit) for more details. |
| `$offset` | `int\|`[`yii\db\Expression`](/docs/api/yii/db/expression/index.md) | The offset number. See [`Query::offset()`](/docs/api/yii/db/querytrait/index.md#method-offset) for more details. |

**Returns**

`string`: SQL statement for OFFSET/FETCH clauses.

### defaultExpressionBuilders() <span id="method-defaultExpressionBuilders"></span>

```php
protected function defaultExpressionBuilders(): array
```

Contains array of default expression builders. Extend this method and override it, if you want to change
default expression builders for this query builder. See [`$expressionBuilders`](/docs/api/yii/db/querybuilder/index.md#property-expressionBuilders) docs for details.

**Returns**

`array`

**Since**

2.0.14

### hasLimit() <span id="method-hasLimit"></span>

```php
protected function hasLimit(mixed $limit): bool
```

Checks to see if the given limit is effective.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$limit` | `mixed` | The given limit |

**Returns**

`bool`: Whether the limit is effective

### hasOffset() <span id="method-hasOffset"></span>

```php
protected function hasOffset(mixed $offset): bool
```

Checks to see if the given offset is effective.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$offset` | `mixed` | The given offset |

**Returns**

`bool`: Whether the offset is effective

### prepareInsertValues() <span id="method-prepareInsertValues"></span>

```php
protected function prepareInsertValues(mixed $table, mixed $columns, mixed $params = []): array
```

Prepares a `VALUES` part for an `INSERT` SQL statement.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$table` | `mixed` | The table that new rows will be inserted into. |
| `$columns` | `mixed` | The column data (name => value) to be inserted into the table or instance of [Query](/docs/api/yii/db/query/index.md) to perform INSERT INTO ... SELECT SQL statement. |
| `$params` | `mixed` | The binding parameters that will be generated by this method. They should be bound to the DB command later. |

**Returns**

`array`: Array of column names, placeholders, values and params.

**Since**

2.0.14