SelectQueryBuilder
Builder for constructing SELECT queries with type-safe column selection, joins, filtering, and more.Type Parameters
DB- The database schema typeTB- Union of table names available in the FROM clauseO- The output row type (columns selected)
Methods
select
select('id').select('first_name') is the same as select(['id', 'first_name']).
Examples:
Select a single column:
selectAll
select * or select table.* clause to the query.
Examples:
Select all columns:
where
WhereInterface documentation for more examples.
whereRef
innerJoin
leftJoin
innerJoin but adds a left join instead of an inner join.
rightJoin
innerJoin but adds a right join instead of an inner join.
fullJoin
innerJoin but adds a full join instead of an inner join.
Only supported by some dialects like PostgreSQL, MS SQL Server and SQLite.
orderBy
OrderByInterface documentation for more examples.
groupBy
having
HavingInterface documentation for more examples.
limit
offset
distinct
distinctOn
union
unionAll
intersect
intersectAll
except
exceptAll
as
$call
this as the only argument.
Example:
$if
func(this) if condition is true.
This method is especially handy with optional selects. Selections made inside the callback add optional fields to the result type.
Example:
$castTo
$narrowType
execute
executeTakeFirst
undefined if the query returned no result.
executeTakeFirstOrThrow
NoResultError is thrown.