Get Started

Upgrade

Where Syntax

Query

Aggregation

Fetch

Transaction

Management

Advanced

Raw object

PDO object

Debug

Information

version: 2.2.0

Raw object

Medoo provided a way to use raw expressions for more complex and customizable queries. It also supports data placeholders 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 use <name> to explain this is the column name. Medoo will handle the rest.

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

For Column

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

  1. $data = $database->get('account', [
  2. 'user_name',
  3. 'score' => Medoo::raw('SUM(<age> + <experience>)')
  4. ], [
  5. 'user_id' => 100
  6. ]);
  1. SELECT "user_name", SUM("age" + "experience") AS "score"
  2. FROM "WP_account"
  3. WHERE "user_id" > 100

For Update and Insert Statement

  1. $data = $database->insert('account', [
  2. 'user_name' => 'apple',
  3. 'user_id' => Medoo::raw('UUID()')
  4. ]);
  5.  
  6. $data = $database->update('account', [
  7. 'user_name' => 'apple',
  8. 'user_id' => Medoo::raw('UUID()')
  9. ], [
  10. 'age[>]' => 10
  11. ]);

For Where Statement

  1. $data = $database->select('account', [
  2. 'user_name',
  3. 'user_id',
  4. ], [
  5. 'datetime' => Medoo::raw('NOW()'),
  6. 'ORDER' => Medoo::raw('RAND()'),
  7. 'LIMIT' => Medoo::raw('AVG(<age>)')
  8. ]);

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.

  1. $today = "2017-05-01";
  2.  
  3. $database->select('account', [
  4. 'user_id',
  5. 'user_name'
  6. ], [
  7. 'datetime' => Medoo::raw('DATE_ADD(:today, INTERVAL 10 DAY)', [
  8. ':today' => $today
  9. ])
  10. ]);

As Where Clause

You can also use raw objects as the where clause for building complex clauses.

  1. $data = $database->select('account', [
  2. 'user_id',
  3. 'email'
  4. ],
  5. Medoo::raw('WHERE
  6. LENGTH(<user_name>) > 5
  7. ORDER BY RAND()
  8. LIMIT 10
  9. ')
  10. );