Joins
Join clauses range from simple to complex including joining complete subqueries on multiple conditions. qb has your back with all of these use cases.
Table of Contents
join
Name
Type
Required
Default
Description
table
true
​
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
type
string
false
"inner"
where
boolean
false
false
Applies a join to the query. The simplest join is to a table based on two columns:
When doing a simple join using =
as the operator, you can omit it and pass just the column names:
Using raw
will most likely tie your code to a specific database, so think carefully before using the raw
method if you want your project to be database agnostic.
When you need to specify more clauses to join, you can pass a function as the second argument:
Conditions inside a join clause can be grouped using a function.
joinWhere
Name
Type
Required
Default
Description
table
string
true
​
The raw SQL string to use as the table.
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
type
string
false
"inner"
Adds a join to another table based on a WHERE
clause instead of an ON
clause. WHERE
clauses introduce parameters and parameter bindings whereas on
clauses join between columns and don't need parameter bindings.
For simple joins, this specifies a column on which to join the two tables:
joinRaw
Name
Type
Required
Default
Description
table
string
true
​
The raw SQL string to use as the table.
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
type
string
false
"inner"
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
Using joinRaw
will most likely tie your code to a specific database, so think carefully before using the joinRaw
method if you want your project to be database agnostic.
joinSub
Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
​
Either a QueryBuilder
instance or a function to define the derived query.
first
true
operator
string
false
"="
The boolean operator for the join clause.
second
false
type
string
false
"inner"
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
Alternatively, a function may be used to define the derived table:
Complex join conditions are also possible by passing a function as the third parameter:
leftJoin
Name
Type
Required
Default
Description
table
true
​
first
string | Expression | Function
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
leftJoinRaw
Name
Type
Required
Default
Description
table
string
true
​
The raw SQL string to use as the table.
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
Using leftJoinRaw
will most likely tie your code to a specific database, so think carefully before using the leftJoinRaw
method if you want your project to be database agnostic.
leftJoinSub
Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
​
Either a QueryBuilder
instance or a function to define the derived query.
first
true
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
rightJoin
Name
Type
Required
Default
Description
table
true
​
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
rightJoinRaw
Name
Type
Required
Default
Description
table
string
true
​
The raw SQL string to use as the table.
first
false
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
Using rightJoinRaw
will most likely tie your code to a specific database, so think carefully before using the rightJoinRaw
method if you want your project to be database agnostic.
rightJoinSub
Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
​
Either a QueryBuilder
instance or a function to define the derived query.
first
true
operator
string
false
"="
The boolean operator for the join clause.
second
false
where
boolean
false
false
Sets if the value of second
should be interpreted as a column or a value. Passing this as an argument is discouraged. Use a closure to define the where clauses where possible.
crossJoin
Name
Type
Required
Default
Description
table
true
​
crossJoinRaw
Name
Type
Required
Default
Description
table
string
true
​
The raw SQL string to use as the table.
Uses the raw SQL provided to as the table for the cross join clause. Cross joins cannot be further constrained with on
or where
clauses.
Using crossJoinRaw
will most likely tie your code to a specific database, so think carefully before using the crossJoinRaw
method if you want your project to be database agnostic.
crossJoinSub
Name
Type
Required
Default
Description
alias
string
true
The alias for the derived table.
input
Function | QueryBuilder
true
​
Either a QueryBuilder
instance or a function to define the derived query.
newJoin
Name
Type
Required
Default
Description
table
true
​
type
string
false
"inner"
The type of the join. Valid types are inner
, left
, right
, or cross
.
JoinClause
on
Name
Type
Required
Default
Description
first
false
operator
string
false
"="
The boolean operator for the condition.
second
false
combinator
string
false
"and"
The boolean combinator for the clause (e.g. "and" or "or").
Applies a join condition to the JoinClause
. An alias for whereColumn
.
orOn
Name
Type
Required
Default
Description
first
false
operator
string
false
"="
The boolean operator for the condition.
second
false
Applies a join condition to the JoinClause
using an or
combinator. An alias for orWhereColumn
.
Preventing Duplicate Joins
You can optionally configure qb to ignore duplicate joins. With this setting turned on each JoinClause
is inspected and checked if it matches any existing JoinClause
instances on the query. This is useful if you have a table shared between optional constraints and want to ensure it is only added once.
You can opt-in to this behavior by setting preventDuplicateJoins = true
in your moduleSettings
in config/ColdBox.cfc
.
Was this helpful?