Search…
Wheres
Table of Contents
where
andWhere
orWhere
whereIn
whereRaw
whereNull

Where Methods

where

Name
Type
Required
Default
Description
column
string | Expression | Function
true
The name of the column or Expression with which to constrain the query. A function can be passed to begin a nested where statement.
operator
string | Expression
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 ) ).
value
any
false
The value with which to constrain the column. An Expression can be passed as well. If a QueryBuilder or Function is passed, it will be used as a subselect expression.
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 andWhere and orWhere methods instead.
Adds a where clause to a query.
QueryBuilder
1
query.from( "users" )
2
.where( "active", "=", 1 );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `active` = ?
Copied!
Using the where method will parameterize the value passed. If you want to constrain a column to another column, use the whereColumn method.
You can also pass an Expression as the value.
QueryBuilder
1
query.from( "users" )
2
.where( "last_logged_in", ">", query.raw( "NOW()" ) );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `last_logged_in` > NOW()
Copied!
Any of the following operators can be used in a where clause.
Valid Operators
=
<
>
<=
>=
<>
!=
like
like binary
not like
between
ilike
&
|
^
<<
>>
rlike
regexp
not regexp
~
~*
!~
!~*
similar to
not similar to
When using the "=" constraint, you can use a shortcut and define the value as the second argument.
QueryBuilder
1
query.from( "users" )
2
.where( "active", 1 );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `active` = ?
Copied!
You may also use dynamic where{Column} statements to simplify this further.
To group where statements together, pass a function to the where clause as the only parameter.
QueryBuilder
1
query.from( "users" )
2
.where( function( q ) {
3
q.where( "active", 1 )
4
.where( "last_logged_in", ">", dateAdd( "ww", -1, now() ) )
5
} );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE (
4
`active` = ?
5
AND
6
`last_logged_in` > ?
7
)
Copied!
This grouping can be nested as many levels as you require.
A Function or QueryBuilder can be used as a subselect expression when passed to value.
QueryBuilder
1
query.from( "users" )
2
.where( "email", "foo" )
3
.orWhere( "id", "=", function( q ) {
4
q.select( q.raw( "MAX(id)" ) )
5
.from( "users" )
6
.where( "email", "bar" );
7
} );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `email` = ?
4
OR `id` = (
5
SELECT MAX(id)
6
FROM `users`
7
WHERE `email` = ?
8
)
Copied!

andWhere

Name
Type
Required
Default
Description
column
string | Expression | Function
true
The name of the column or Expression with which to constrain the query. A function can be passed to begin a nested where statement.
operator
string | Expression
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 ) ).
value
any
false
The value with which to constrain the column. An Expression can be passed as well. If a QueryBuilder or Function is passed, it will be used as a subselect expression.
This method is simply an alias for where with the combinator set to "and".

orWhere

Name
Type
Required
Default
Description
column
string | Expression | Function
true
The name of the column or Expression with which to constrain the query. A function can be passed to begin a nested where statement.
operator
string | Expression
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 ) ).
value
any
false
The value with which to constrain the column. An Expression can be passed as well. If a QueryBuilder or Function is passed, it will be used as a subselect expression.
This method is simply an alias for where with the combinator set to "or".

whereBetween

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
start
any | Function | QueryBuilder
true
The beginning value of the BETWEEN statement. If a function or QueryBuilder is passed it is used as a subselect expression.
end
any | Function | QueryBuilder
true
The end value of the BETWEEN statement. If a function or QueryBuilder is passed it is used as a subselect expression.
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 andWhere and orWhere dynamic methods instead.
negate
boolean
false
false
False for BETWEEN, True for NOT BETWEEN.
Adds a where between clause to the query.
QueryBuilder
1
query.from( "users" )
2
.whereBetween( "id", 1, 2 );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `id` BETWEEN ? AND ?
Copied!
If a function or QueryBuilder is passed it is used as a subselect expression.
QueryBuilder
1
query.from( "users" )
2
.whereBetween(
3
"id",
4
function( q ) {
5
q.select( q.raw( "MIN(id)" ) )
6
.from( "users" )
7
.where( "email", "bar" );
8
},
9
builder.newQuery()
10
.select( builder.raw( "MAX(id)" ) )
11
.from( "users" )
12
.where( "email", "bar" )
13
);
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `id` BETWEEN (
4
SELECT MIN(id)
5
FROM `users`
6
WHERE `email` = ?
7
)
8
AND (
9
SELECT MAX(id)
10
FROM `users`
11
WHERE `email` = ?
12
)
Copied!

whereNotBetween

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
start
any | Function | QueryBuilder
true
The beginning value of the BETWEEN statement. If a function or QueryBuilder is passed it is used as a subselect expression.
end
any | Function | QueryBuilder
true
The end value of the BETWEEN statement. If a function or QueryBuilder is passed it is used as a subselect expression.
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 andWhere and orWhere dynamic methods instead.
Adds a where not in clause to the query. This behaves identically to the whereBetween method with the negateflag set to true. See the documentation for whereBetween for usage and examples.

whereColumn

Name
Type
Required
Default
Description
first
string | Expression
true
The name of the first column or Expression with which to constrain the query.
operator
string | Expression
true
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 ) ).
second
string | Expression
false
The name of the second column or Expression with which to constrain the query.
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 andWhere and orWhere dynamic methods instead.
Adds a where clause to a query that compares two columns.
QueryBuilder
1
query.from( "users" )
2
.whereColumn( "first_name", "=", "last_name" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `first_name` = `last_name`
Copied!
Just as with where, when using "=" as the operator you can use a shorthand passing the second column in as the operator and leaving the second column null.
QueryBuilder
1
query.from( "users" )
2
.whereColumn( "first_name", "last_name" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `first_name` = `last_name`
Copied!
Expressions can be passed in place of either column.
QueryBuilder
1
query.from( "users" )
2
.whereColumn( "first_name", query.raw( "LOWER(first_name)" ) );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `first_name` = LOWER(first_name)
Copied!

whereExists

Name
Type
Required
Default
Description
query
Function | QueryBuilder
true
A function or QueryBuilder instance to be used as the exists subquery.
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 andWhere and orWhere dynamic methods instead.
negate
boolean
false
false
False for EXISTS, True for NOT EXISTS.
Adds a where exists clause to the query.
It can be configured with a function.
QueryBuilder
1
query.from( "orders" )
2
.whereExists( function( q ) {
3
q.select( q.raw( 1 ) )
4
.from( "products" )
5
.whereColumn( "products.id", "orders.id" );
6
} );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE EXISTS (
4
SELECT 1
5
FROM `products`
6
WHERE `products`.`id` = `orders`.`id`
7
)
Copied!
It can also be configured with a QueryBuilder instance.
QueryBuilder
1
var existsQuery = query.newQuery()
2
.select( q.raw( 1 ) )
3
.from( "products" )
4
.whereColumn( "products.id", "orders.id" );
5
6
query.from( "orders" )
7
.whereExists( existsQuery );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE EXISTS (
4
SELECT 1
5
FROM `products`
6
WHERE `products`.`id` = `orders`.`id`
7
)
Copied!

whereNotExists

Name
Type
Required
Default
Description
query
Function | QueryBuilder
true
A function or QueryBuilder instance to be used as the not exists subquery.
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 andWhere and orWhere dynamic methods instead.
Adds a where not in clause to the query. This behaves identically to the whereExists method with the negateflag set to true. See the documentation for whereExists for usage and examples.

whereLike

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
value
any
false
The value with which to constrain the column. An Expression can be passed as well. If a QueryBuilder or Function is passed, it will be used as a subselect expression.
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 andWhere and orWhere dynamic methods instead.
A shortcut for calling where with "like" set as the operator.
QueryBuilder
1
query.from( "users" )
2
.whereLike( "username", "J%" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `username` LIKE ?
Copied!

whereNotLike

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
value
any
false
The value with which to constrain the column. An Expression can be passed as well. If a QueryBuilder or Function is passed, it will be used as a subselect expression.
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 andWhere and orWhere dynamic methods instead.
A shortcut for calling where with "not like" set as the operator.
QueryBuilder
1
query.from( "users" )
2
.whereNotLike( "username", "J%" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `username` NOT LIKE ?
Copied!

whereIn

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
values
string | array | Expression | Function | QueryBuilder
true
A single value, list of values, or array of values to constrain a column with. Expressions may be used in any place a value is used. Alternatively, a function or QueryBuilder instance can be passed in to be used as a subquery expression.
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 andWhere and orWhere dynamic methods instead.
negate
boolean
false
false
False for IN, True for NOT IN.
Adds a where in clause to the query.
The values passed to whereIn can be a single value, a list of values, or an array of values.
QueryBuilder
1
query.from( "orders" )
2
.whereIn( "id", [ 1, 4, 66 ] );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE `id` IN (?, ?, ?)
Copied!
Some database grammars have a hard limit on the number of parameters passed to a SQL statement. Keep this in mind while writing your queries.
If a list of values is passed in, it is converted to an array of values using a single comma (",") delimiter.
QueryBuilder
1
query.from( "orders" )
2
.whereIn( "id", "1,4,66" );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE `id` IN (?, ?, ?)
Copied!
Any value in the list or array can also be passed using a custom parameter type to have more control over the parameter settings.
QueryBuilder
1
query.from( "orders" )
2
.whereIn( "id", [ 1, 4, { value = "66", cfsqltype = "CF_SQL_VARCHAR" } ] );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE `id` IN (?, ?, ?)
Copied!
Expressions can be freely mixed in with other values.
QueryBuilder
1
query.from( "orders" )
2
.whereIn( "id", [ query.raw( "MAX(id)" ), 4, 66 ] );
Copied!
MySQL
1
SELECT *
2
FROM `orders`
3
WHERE `id` IN (MAX(id), ?, ?)
Copied!
A function or QueryBuilder instance can be passed to be used as a subquery expression instead of a list of values.
QueryBuilder
1
query.from( "users" )
2
.whereIn( "id", function( q ) {
3
q.select( "id" )
4
.from( "users" )
5
.where( "age", ">", 25 );
6
} );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE IN (
4
SELECT `id`
5
FROM `users`
6
WHERE `age` > ?
7
)
Copied!
You may find a whereExists method performs better for you than a whereIn with a subquery.

whereNotIn

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column or Expression with which to constrain the query.
values
string | array | Expression | Function | QueryBuilder
true
A single value, list of values, or array of values to constrain a column with. Expressions may be used in any place a value is used. Alternatively, a function or QueryBuilder instance can be passed in to be used as a subquery expression.
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 andWhere and orWhere dynamic methods instead.
Adds a where not in clause to the query. This behaves identically to the whereIn method with the negateflag set to true. See the documentation for whereIn for usage and examples.

whereRaw

Name
Type
Required
Default
Description
sql
string
true
The raw SQL to add to the query.
whereBindings
array
false
[]
Any bindings needed for the raw SQL. Bindings can be simple values or custom parameters.
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 andWhere and orWhere dynamic methods instead.
Shorthand to add a raw SQL statement to the where clauses.
QueryBuilder
1
query.from( "users" )
2
.whereRaw(
3
"id = ? OR email = ? OR is_admin = 1",
4
[ 1, "foo" ]
5
);
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE id = ? OR email = ? OR is_admin = 1
Copied!

whereNull

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column to check if it is NULL. Can also pass an Expression.
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 andWhere and orWhere dynamic methods instead.
negate
boolean
false
false
False for NULL, True for NOT NULL.
Adds a where null clause to the query.
QueryBuilder
1
query.from( "users" )
2
.whereNull( "id" );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `id` IS NULL
Copied!

whereNotNull

Name
Type
Required
Default
Description
column
string | Expression
true
The name of the column to check if it is NULL. Can also pass an Expression.
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 andWhere and orWhere dynamic methods instead.
negate
boolean
false
false
False for NULL, True for NOT NULL.
Adds a where not in clause to the query. This behaves identically to the whereNull method with the negateflag set to true. See the documentation for whereNull for usage and examples.

Dynamic Where Methods

qb uses onMissingMethod to provide a few different helpers when working with where... methods.

andWhere... and orWhere...

Every where... method in qb can be called prefixed with either and or or. Doing so will call the original method using the corresponding combinator.
QueryBuilder
1
query.from( "users" )
2
.where( "username", "like", "j%" )
3
.andWhere( function( q ) {
4
q.where( "isSubscribed", 1 )
5
.orWhere( "isOnFreeTrial", 1 );
6
} );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `username` LIKE ?
4
AND (
5
`isSubscribed` = ?
6
OR
7
`isOnFreeTrial` = ?
8
)
Copied!

where{Column}

If you call a method starting with where that does not match an existing qb method, qb will instead call the where method using the rest of the method name as the first column name. (The rest of the arguments will be shifted to account for this.) This also applies to andWhere{Column} and orWhere{Column} method signatures.
QueryBuilder
1
query.from( "users" )
2
.whereUsername( "like", "j%" )
3
.whereActive( 1 );
Copied!
MySQL
1
SELECT *
2
FROM `users`
3
WHERE `username` LIKE ?
4
AND `active` = ?
Copied!
Last modified 4mo ago