EN English
Version: v2.4.0

raw

Medoo provides raw expressions for complex and highly customizable queries. Raw expressions also support placeholders to prevent SQL injection, plus shortcut syntax for identifier quoting.

Medoo::raw($query, $map)

Syntax

Raw SQL expressions provide a shortcut for quoting identifiers. Use <name> to mark a column (or table-qualified column), and Medoo handles quoting automatically.
Medoo::raw("AVG(<weight>)")

For Column

You can use a raw object in the select() column list. The array key becomes the alias in the result.
$data = $database->get('account', [
	'user_name',
	'score' => Medoo::raw('SUM(<age> + <experience>)')
], [
	'user_id' => 100
]);
SELECT "user_name", SUM("age" + "experience") AS "score"
FROM "WP_account"
WHERE "user_id" = 100

For Update and Insert Statements

Raw objects can also be used as column values in insert() and update().
$data = $database->insert('account', [
	'user_name' => 'apple',
	'user_id' => Medoo::raw('UUID()')
]);

$data = $database->update('account', [
	'user_name' => 'apple',
	'user_id' => Medoo::raw('UUID()')
], [
	'age[>]' => 10
]);

For Where Clauses

Raw objects can be used inside the where array for advanced expressions.
$data = $database->select('account', [
	'user_name',
	'user_id',
], [
	'datetime[>=]' => Medoo::raw('DATE_SUB(NOW(), INTERVAL 1 DAY)'),
	'ORDER' => Medoo::raw('RAND()'),
	'LIMIT' => 10
]);
SELECT "user_name", "user_id"
FROM "account"
WHERE "datetime" >= DATE_SUB(NOW(), INTERVAL 1 DAY)
ORDER BY RAND()
LIMIT 10

With Prepared Statement

If raw expressions include values from variables or user input, use prepared placeholders to prevent SQL injection.
$today = "2017-05-01";

$database->select('account', [
	'user_id',
	'user_name'
], [
	'datetime' => Medoo::raw('DATE_ADD(:today, INTERVAL 10 DAY)', [
		':today' => $today
	])
]);

As Where Clause

You can also use a raw object as the full where clause for advanced filtering.
$data = $database->select('account', [
		'user_id',
		'email'
	],
	Medoo::raw('WHERE
		LENGTH(<user_name>) > 5
		ORDER BY RAND()
		LIMIT 10
	')
);
SELECT "user_id", "email"
FROM "account"
WHERE LENGTH("user_name") > 5
ORDER BY RAND()
LIMIT 10