Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Advanced

Raw object

PDO object

Debug

Information

version: 1.5.7

Raw object

Medoo provided a way to use raw expression for more complex and customizable query, especially while using SQL function. It's also supported data placeholder for preventing SQL injection and optimized syntax without carrying about quotation.

Medoo::raw($query, $map)

Syntax

The raw SQL expression provided a shortcut for quoting column name, so you don't have to care about the quotation. All you have to do is using <name> to explain this is column name. Medoo will handle the rest.

Medoo::raw("AVG(<weight>)")

For Column

You can use raw object to select() column name parameter. It will be alias as the key name.

$data = $database->get('account', [
	'user_name',
	'user_id',
	'score' => Medoo::raw('SUM(<age> + <experience>)')
], [
	'user_id' => 100
]);

// SELECT "user_name", "user_id", SUM("age" + "experience") AS "score"
// FROM "WP_account"
// WHERE "user_id" > 100

For Update and Insert Statement

$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 Statement

$data = $database->select('account', [
	'user_name',
	'user_id',
], [
	'datetime' => Medoo::raw('NOW()'),
	'ORDER' => Medoo::raw('RAND()'),
	'LIMIT' => Medoo::raw('AVG(<age>)')
]);

With Prepared Statement

If you want to use some values from other variable or user post date for raw object, you can use prepared statement for 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 raw object as where clause for building complex clause.

$data = $database->select('account', [
		'user_id',
		'email'
	],
	Medoo::raw('WHERE
		LENGTH(<user_name>) > 5
		ORDER BY RAND()
		LIMIT 10
	')
);