close
EN English
Version: v2.3.0

WHERE Syntax

Some Medoo methods accept a $where argument to filter records, similar to the SQL WHERE clause. Although SQL conditions can become complex and may introduce SQL injection risks when written manually, Medoo provides a structured and expressive way to build WHERE clauses safely.

Basic Conditions

Basic conditions are straightforward to use. You can also append operators to a column name to create more advanced comparisons, especially for numeric values.
$database->select("account", "user_name", [
	"email" => "foo@bar.com"
]);
// WHERE email = 'foo@bar.com'

$database->select("account", "user_name", [
	"user_id" => 200
]);
// WHERE user_id = 200

$database->select("account", "user_name", [
	"user_id[>]" => 200
]);
// WHERE user_id > 200

$database->select("account", "user_name", [
	"user_id[>=]" => 200
]);
// WHERE user_id >= 200

$database->select("account", "user_name", [
	"user_id[!]" => 200
]);
// WHERE user_id != 200

$database->select("account", "user_name", [
	"age[<>]" => [200, 500]
]);
// WHERE age BETWEEN 200 AND 500

$database->select("account", "user_name", [
	"age[><]" => [200, 500]
]);
// WHERE age NOT BETWEEN 200 AND 500
The [<>] and [><] operators can also be used with datetime values.
$database->select("account", "user_name", [
	"birthday[<>]" => [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d")]
]);

$database->select("account", "user_name", [
	"birthday[><]" => [date("Y-m-d", mktime(0, 0, 0, 1, 1, 2015)), date("Y-m-d")]
]);
WHERE ("birthday" BETWEEN '2015-01-01' AND '2017-01-01')
WHERE ("birthday" NOT BETWEEN '2015-01-01' AND '2017-01-01')
You can pass not only a single string or numeric value, but also an array. In that case, Medoo generates an IN condition.
$database->select("account", "user_name", [
	"OR" => [
		"user_id" => [2, 123, 234, 54],
		"email" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"]
	]
]);
WHERE
user_id IN (2,123,234,54) OR
email IN ('foo@bar.com','cat@dog.com','admin@medoo.in')

Negative Conditions

Use the [!] operator to build negative comparisons such as !=, NOT IN, and IS NOT NULL.
$database->select("account", "user_name", [
	"AND" => [
		"user_name[!]" => "foo",
		"user_id[!]" => 1024,
		"email[!]" => ["foo@bar.com", "cat@dog.com", "admin@medoo.in"],
		"city[!]" => null,
		"promoted[!]" => true
	]
]);
WHERE
"user_name" != 'foo' AND
"user_id" != 1024 AND
"email" NOT IN ('foo@bar.com','cat@dog.com','admin@medoo.in') AND
"city" IS NOT NULL AND
"promoted" != 1
You can also use the result of select() or get() as the value of a condition.
$database->select("account", "user_name", [
	"user_id" => $database->select("post", "user_id", ["comments[>]" => 40])
]);
WHERE user_id IN (2, 51, 321, 3431)

Logical Conditions

Logical conditions describe relationships between multiple expressions. Use AND and OR to build more complex queries.
Basic Usage
$database->select("account", "user_name", [
	"AND" => [
		"user_id[>]" => 200,
		"age[<>]" => [18, 25],
		"gender" => "female"
	]
]);

// Medoo combines conditions with AND by default. The query below is equivalent.
$database->select("account", "user_name", [
	"user_id[>]" => 200,
	"age[<>]" => [18, 25],
	"gender" => "female"
]);
WHERE user_id > 200 AND age BETWEEN 18 AND 25 AND gender = 'female'
$database->select("account", "user_name", [
	"OR" => [
		"user_id[>]" => 200,
		"age[<>]" => [18, 25],
		"gender" => "female"
	]
]);
WHERE user_id > 200 OR age BETWEEN 18 AND 25 OR gender = 'female'
Nested Conditions
$database->has("account", [
	"AND" => [
		"OR" => [
			"user_name" => "foo",
			"email" => "foo@bar.com"
		],
		"password" => "12345"
	]
]);
WHERE (user_name = 'foo' OR email = 'foo@bar.com') AND password = '12345'

Because Medoo uses arrays to describe logical conditions, duplicate keys in the same array will overwrite each other.

// This will not work as expected.
$database->select("account", '*', [
	"AND" => [
		"OR" => [
			"user_name" => "foo",
			"email" => "foo@bar.com"
		],
		"OR" => [
			"user_name" => "bar",
			"email" => "bar@foo.com"
		]
	]
]);
// [X] SELECT * FROM "account" WHERE ("user_name" = 'bar' OR "email" = 'bar@foo.com')
To avoid key collisions, append a comment after the logical key name. The comment starts with # and can contain any text.
$database->select("account", '*', [
	"AND #Comments can be used with any AND or OR logical condition" => [
		"OR #the first condition" => [
			"user_name" => "foo",
			"email" => "foo@bar.com"
		],
		"OR #the second condition" => [
			"user_name" => "bar",
			"email" => "bar@foo.com"
		]
	]
]);
WHERE (
	("user_name" = 'foo' OR "email" = 'foo@bar.com')
	AND
	("user_name" = 'bar' OR "email" = 'bar@foo.com')
)
Column Relationships
$database->select("post", [
	"[>]account" => "user_id",
], [
	"post.content"
], [
	// Compare columns with operators on fully qualified column names, such as [=], [>], [<], and [!=].
	"post.restrict[<]account.age"
]);
WHERE "post"."restrict" < "account"."age"

LIKE Conditions

Use the [~] operator to build LIKE conditions. It can be used in both basic and logical conditions.
// By default, the keyword is wrapped with % on both sides to match the whole word.
$database->select("person", "id", [
	"city[~]" => "lon"
]);
WHERE "city" LIKE '%lon%'
Group Matching
$database->select("person", "id", [
	"city[~]" => ["lon", "foo", "bar"]
]);
WHERE "city" LIKE '%lon%' OR "city" LIKE '%foo%' OR "city" LIKE '%bar%'
Negative LIKE Conditions
$database->select("person", "id", [
	"city[!~]" => "lon"
]);
WHERE "city" NOT LIKE '%lon%'
Compound LIKE Conditions
$database->select("person", "id", [
	"content[~]" => ["AND" => ["lon", "on"]]
]);
WHERE ("content" LIKE '%lon%' AND "content" LIKE '%on%')
$database->select("person", "id", [
	"content[~]" => ["OR" => ["lon", "on"]]
]);
WHERE ("content" LIKE '%lon%' OR "content" LIKE '%on%')
SQL Wildcards
You can use SQL wildcard patterns for more advanced matching.
$database->select("person", "id", [
	"city[~]" => "%stan" // Kazakhstan, Uzbekistan, Turkmenistan
]);

$database->select("person", "id", [
	"city[~]" => "Londo_" // London, Londox, Londos, ...
]);

$database->select("person", "id", [
	"name[~]" => "[BCR]at" // Bat, Cat, Rat
]);

$database->select("person", "id", [
	"name[~]" => "[!BCR]at" // Eat, Fat, Hat, ...
]);

Order Conditions

Use ORDER to sort the result set.
// Sort by a single column.
$database->select("account", "user_id", [
	"ORDER" => "user_id"
]);

// Sort by multiple columns.
$database->select("account", "user_id", [
	"ORDER" => [
		// Sort a column using a custom order.
		"user_id" => [43, 12, 57, 98, 144, 1],

		// Sort by a column using the default direction.
		"register_date",

		// Sort in descending order.
		"profile_id" => "DESC",

		// Sort in ascending order.
		"date" => "ASC"
	]
]);

Full-Text Search

Full-text search is supported by MySQL for more advanced search queries.
Search Modes
natural IN NATURAL LANGUAGE MODE
natural+query IN NATURAL LANGUAGE MODE WITH QUERY EXPANSION
boolean IN BOOLEAN MODE
query WITH QUERY EXPANSION
// MATCH condition.
$database->select("post_table", "post_id", [
	"MATCH" => [
		"columns" => ["content", "title"],
		"keyword" => "foo",

		// Optional: search mode.
		"mode" => "natural"
	]
]);
WHERE MATCH (content, title) AGAINST ('foo' IN NATURAL LANGUAGE MODE)

Regular Expressions

Use the [REGEXP] operator to match values with a regular expression.
$data = $database->select('account', [
	'user_id',
	'user_name'
], [
	'user_name[REGEXP]' => '[a-z0-9]*'
]);
WHERE "user_name" REGEXP '[a-z0-9]*'

SQL Functions

For advanced cases, you can use SQL functions through the raw object. Read more in the raw documentation.
$data = $database->select('account', [
	'user_id',
	'user_name'
], [
	'datetime' => Medoo::raw('NOW()')
]);
WHERE "datetime" = NOW()

LIMIT and OFFSET

Use LIMIT to restrict the number of rows returned. You can also specify an offset.
// Return the first 100 rows.
$database->select("account", "user_id", [
	"LIMIT" => 100
]);

// Skip the first 20 rows, then return the next 100.
$database->select("account", "user_id", [
	"LIMIT" => [20, 100]
]);

// For Oracle and MSSQL, ORDER BY is also required.
$database->select("account", "user_id", [
	"LIMIT" => [20, 100],
	"ORDER" => "location"
]);

GROUP and HAVING

Use GROUP to group rows, and HAVING to filter grouped results.
// Group by a single column.
$database->select("account", "user_id", [
	"GROUP" => "type"
]);

// Group by multiple columns.
$database->select("account", "user_id", [
	"GROUP" => [
		"type",
		"age",
		"gender"
	]
]);

// Group rows and filter the grouped result.
$database->select("account", "user_id", [
	"GROUP" => "type",
	"HAVING" => [
		"user_id[>]" => 500
	]
]);