Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Management

Advanced

Raw object

PDO object

Debug

Information

version: 2.0.0

Raw object

Medoo provided a way to use raw expressions for more complex and customizable queries. It's also supported data placeholder for preventing SQL injection and optimized syntax without carrying about the quotation.

Medoo::raw($query, $map)

Syntax

The raw SQL expression provides a shortcut for quoting column names, 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 aliased as the key name.

$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 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 variables or user post dates for the raw object, you can use a prepared statement 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 raw objects as where clause for building complex clauses.

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