Where Clauses
Last updated
Last updated
By default, the correct sql type will be inferred from your parameters. If you pass a number, CF_SQL_NUMERIC
will be used. If it is a date, CF_SQL_TIMESTAMP
, and so forth. If you need more control, you can always pass a struct with the parameters you would pass to cfqueryparam
.
You may use the where
method on a query builder instance to add where
clauses to the query. The most basic call to where
requires three arguments. The first argument is the name of the column. The second argument is an operator, which can be any of the database's supported operators. Finally, the third argument is the value to evaluate against the column.
For example, here is a query that verifies the value of the "age" column is greater than or equal to 18:
For convenience, if you simply want to verify that a column is equal to a given value, you may pass the value directly as the second argument to the where
method:
Of course, you may use a variety of other operators when writing a where
clause:
You may chain where constraints together as well as add or
clauses to the query. The orWhere
method accepts the same arguments as the where
method:
whereBetween / whereNotBetween
The whereBetween
method verifies that a column's value is between two values:
The whereNotBetween
method verifies that a column's value lies outside of two values:
whereIn / whereNotIn (sub-queries)
The whereIn
method verifies that a given column's value is contained within the provided array or QueryBuilder object:
Array:
QueryBuilder (fetch all users whose age is in the all_ages table with a value between 17 and 21):
The whereNotIn
method verifies that the given column's value is not contained in the provided array of QueryBuilder object:
whereNull / whereNotNull
The whereNull
method verifies that the value of the given column is NULL
:
The whereNotNull
method verifies that the column's value is not NULL
:
whereExists / whereNotExists
The whereExists
method:
whereColumn
The whereColumn
method may be used to verify that two columns are equal:
You may also pass a comparison operator to the method:
WHERE (a = ? OR b = ?) AND c = ?
Here is an example of how to strategically place parentheses with OR
using closures.
Base
AND
OR
where()
andWhere()
orWhere()
whereBetween()
andWhereBetween()
orWhereBetween()
whereColumn()
andWhereColumn()
orWhereColumn()
whereExists()
andWhereExists()
orWhereExists()
whereIn()
andWhereIn()
orWhereIn()
whereNotBetween()
andWhereNotBetween()
orWhereNotBetween()
whereNotExists()
andWhereNotExists()
orWhereNotExists()
whereNotIn()
andWhereNotIn()
orWhereNotIn()
whereNotNull()
andWhereNotNull()
orWhereNotNull()
whereNull()
andWhereNull()
orWhereNull()
whereRaw()
andWhereRaw()
orWhereRaw()