Skip to content

Instantly share code, notes, and snippets.

@bleeDev
Last active December 25, 2015 13:19
Show Gist options
  • Save bleeDev/6982685 to your computer and use it in GitHub Desktop.
Save bleeDev/6982685 to your computer and use it in GitHub Desktop.
Drupal 7 Database API example
$result = db_query(SELECT a.field_1, b.field_2 as second_field, CONCAT('My', 'S', 'QL') as mysql FROM table_1 a LEFT OUTER JOIN table_2 b ON b.id = a.id WHERE a.field_3 >= 25 and a.field_4 = 'this' ORDER BY a.field_5 DESC);
// Would be equivalent to.
$query = db_select('table_1', 'a');
$query->fields('a', array('field_1'))
->addExpression("CONCAT('My', 'S', 'QL')", 'mysql')
->leftjoin('table_2', 'b', 'b.id = a.id')
->addField('b', 'field_2', 'second_field')
->condition('a.field_3', 25, ' >=')
->condition('a.field_4', 'this')
->orderBy('a.field_5', 'DESC');
$result = $query->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment