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.
Name
Type
Required
Default
Description
columns
string | array
false
"*"
A single column, list of columns, or array of columns to retrieve.
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 "*"
.
Name
Type
Required
Default
Description
state
boolean
false
true
Value to set the distinct flag.
Calling distinct will cause the query to be executed with the DISTINCT
keyword.
Name
Type
Required
Default
Description
columns
string | array
true
A single column, list of columns, or array of columns to add to the select.
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.
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.
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.)
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.
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
No arguments
``
Clears out the selected columns for a query along with any configured select bindings.
Name
Type
Required
Default
Description
columns
string | array
false
"*"
A single column, list of columns, or array of columns to retrieve.
Clears out the selected columns for a query along with any configured select bindings. Then sets a selection of columns to select from the query. Any valid argument to select
can be passed here.
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.
Clears out the selected columns for a query along with any configured select bindings. Then adds an Expression or array of expressions to the already selected columns.
Name
Type
Required
Default
Description
from
string | Expression
true
The name of the table or a Expression object from which the query is based.
Used to set the base table for the query.
You can optionally specify an alias for the table.
Name
Type
Required
Default
Description
table
string | Expression
true
The name of the table or a Expression object from which the query is based.
An alias for from
where you like how calling table
looks.
Name
Type
Required
Default
Description
from
string
true
The sql snippet to use as the table.
bindings
array
false
[]
Any bindings needed for the expression.
Sometimes you need more control over your from
clause in order to add grammar specific instructions, such as adding SQL Server table hints to your queries.
Since the fromRaw()
takes your string verbatim, it's important that you make sure your SQL declaration is escaped properly. Failure to properly escape your table names may result in SQL errors.
Using fromRaw
will most likely tie your code to a specific database, so think carefully before using the fromRaw
method if you want your project to be database agnostic.
Many database engines allow you to define User Defined Functions. For example, SQL Server allows you to define UDFs that will return a table. In these type of cases, it may be necessary to bind parameters to your from
clause.
You can bind parameters to the fromRaw()
method by passing a secondary argument that is an array of the parameters to bind.
Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
Either a QueryBuilder
instance or a closure to define the derived query.
Complex queries often contain derived tables. Derived tables are essentially a temporal table defined as a subquery in the from
statement.
In additional a function callback, a separate QueryBuilder
instance can be passed to the fromSub
method.
alias
string
The new alias to use for the table.
Adds an alias to the specified from
table or renames a current alias. Any existing aliased values in columns
, wheres
, joins
, groupBys
, or orders
that match the previous alias will be remapped to the new alias. This includes the full table name when used as an alias.
Loading...
Loading...
The orderBy
method seems simple but has a lot of depth depending on the type of arguments you pass in.
Name
Type
Required
Default
Description
column
any
true
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"
or "desc"
.
Calling orderBy
multiple times will append to the order list.
Name
Type
Required
Default
Description
column
any
true
The list of the columns to order by. Each column can optionally declare it's sort direction after a pipe delimiter. (e.g. `"height
desc"`).
direction
string
false
"asc"
The direction by which to order the query. Accepts "asc"
or "desc"
. This value will be used as the default value for all entries in the column list that fail to specify a direction for a specific column.
You can order with a subquery using either a function or a QueryBuilder instance.
Name
Type
Required
Default
Description
groups
string | array
true
A single column name, a list of column names, or an array of column names to group by. An Expression
can be passed as well.
Passing a single string will group by that one column.
You can also pass a list of column names. A single comma (","
) will be used as the delimiter.
An array of column names can be provided.
Calling groupBy
multiple times will to the current groups.
An Expression
can be passed in place of a column.
Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression
with which to constrain the query.
operator
any
false
The operator to use for the constraint (i.e. "=", "<", ">=", etc.). A value can be passed as the operator
and the value
left null as a shortcut for equals (e.g. where( "column", 1 ) == where( "column", "=", 1 ) ).
combinator
string
false
"and"
The boolean combinator for the clause. Valid options are "and"
or "or"
. Avoid passing this parameter explicitly. Where possible use the andHaving
and orHaving
methods instead.
Adds a having clause to a query.
Expressions
can be used in place of the column or the value.
Loading...
qb includes a few methods to help you lock certain rows when executing select
statements.
Note: For locks to work properly, they must be nested inside a transaction
. qb does not handle any of the transaction lifecycle for you.
Name
Type
Required
Default
Description
No arguments
A shared lock prevents the selected rows from being modified until your transaction is committed.
skipLocked
Boolean
false
false
A lock for update lock prevents the selected rows from being modified or selected with another shared lock until your transaction is committed.
The main difference between a sharedLock
and lockForUpdate
is that a lockForUpdate
prevents other reads or selects as well as updates.
When using the skipLocked
flag, the query will skip over locked records and only return and lock available records.
Name
Type
Required
Default
Description
No arguments
noLock
will instruct your grammar to ignore any shared locks when executing the query.
Currently this only makes a difference in SQL Server grammars.
Name
Type
Required
Default
Description
value
string
true
The custom lock directive to add to the query.
The lock
method will allow you to add a custom lock directive to your query. Think of it as the raw
method for lock directives.
These lock directives vary from grammar to grammar.
Name
Type
Required
Default
Description
No arguments
Clears any lock directive on the query.
Loading...
Loading...
Loading...
Loading...
Loading...