You may not always want to select all columns from a database table. You can influence the select list of a query with the following methods.
Individual columns can contain fully-qualified names (some_table.some_column
), table aliases (alias.some_column
), and even set column aliases themselves (some_column AS c
). The columns
argument can be a single column, a list of columns (comma-separated), or an array of columns.
When calling select
any previous columns are discarded. If you want to incrementally select columns, use the addSelect
method.
If you pass no columns to this method, it will default to "*"
.
Calling distinct will cause the query to be executed with the DISTINCT
keyword.
distinct
applies to the entire query, not just certain fields.
This method adds the columns passed to it to the currently selected columns.
If the QueryBuilder
is currently selecting all columns ("*"
) when this method is called, the incoming columns will becoming the only columns selected.
A shortcut to use a raw expression in the select clause.
The expression is added to the other already selected columns.
(To learn more about raw and expressions, check out the docs on Raw Expressions.)
The method lets you pass either a callback or a QueryBuilder
instance to be used as a subselect expression. If a callback is passed it will be passed a new query instance as the only parameter.
The subselect is added to the other already selected columns.
Name
Type
Required
Default
Description
columns
string | array
false
"*"
A single column, list of columns, or array of columns to retrieve.
Name
Type
Required
Default
Description
state
boolean
false
true
Value to set the distinct flag.
Name
Type
Required
Default
Description
columns
string | array
true
A single column, list of columns, or array of columns to add to the select.
Name
Type
Required
Default
Description
expression
any
true
The raw expression for the select statement.
bindings
array
false
[]
Any bindings needed for the raw expression.
Name
Type
Required
Default
Description
alias
string
true
The alias for the subselect expression.
query
Function | QueryBuilder
true
The callback or query to use in the subselect.