Drupal 7 Database API Cheat Sheet
Drupal 7 Database API Cheat Sheet
SELECT
return The called query object. return Condition of OR-ed conditions.
db_select($table, $alias = NULL,
$options = array()) ->union(SelectQueryInterface $query, ->db_and()->condition()->condition()
$table Database table to select from. $type = '') return Condition of AND-ed conditions.
$alias Table alias. $query Query to union.
return New query object. $type Type of union. ->isNull($field)
return New resulting query object.
->isNotNull($field)
$query = db_select('users', 'u')
->fields('u', $field The field to check.
->addExpression($expression, $alias
array('uid', 'name')); return The called query object.
= NULL, $arguments = array())
$result = $query->execute();
$expression Expression string.
->exists(SelectQueryInterface
$alias Expression alias.
->distinct($distinct = TRUE) $select);
$arguments Assoc array of placehoders and
$distinct Flag indicating DISTINCT query. placeholder values. ->notExists(SelectQueryInterface
return The called query object. return Unique expression alias. $select);
$select The query to check.
->fields($table_alias, $fields = ->countQuery() return The called query object.
array()) return New query object.
$table_alias Alias of the table the field PAGER
JOIN
belongs to. ->addTag($tag)
$fields Array of field names. ->extend(‘PagerDefault’)
->join($table, $alias = NULL,
$tag Query identification.
return The called query object. $condition = NULL, $arguments = return New pager extender object.
return The called query object.
array())
->addField($table_alias, $field, $table The table to join with. ->extend(‘PagerDefault’)->limit
->hasTag($tag)
$alias = NULL) $alias Table alias. ($count)
$tag Query identification. $condition Join conditions.
$table_alias Alias of the table the field $count Number of items per page.
return TRUE if condition is met. $arguments Assoc array of placeholders and
belongs to.
$field Field name. placeholder values.
return Unique table alias. SORTABLE TABLE
$alias Field alias. CONDITIONS
return Unique field alias.
->extend(‘TableSort’)
->condition($field, $value = NULL, $query = db_select('users', 'u');
$query->innerJoin('node', 'n', return Table extender object.
->range($start = NULL, $length = $operator = NULL)
'n.uid = u.uid'); return The called query object.
NULL) $field The field to check or the result of $query->addField('u', 'name');
$start First record of the result set. a logic operation (or, and, xor) $query->addField('n', 'title'); ->extend(‘TableSort’)->orderByHeader
$length Max number of records. $value The value to test against. $result = $query->execute(); ($header)
return The called query object. $operator
Default: ‘=’ or ‘IN’.
Supported: =, <, >, >=, <=, IN, $header Array with sorting criteria.
NOT IN, LIKE, BETWEEN, IS ->innerJoin ($table, $alias = NULL, return The called query object.
->groupBy($field) $condition = NULL, $arguments =
NULL, IS NOT NULL
$field The field to group by. return The called query object. array()) $header = array(
return The called query object. ->leftJoin ($table, $alias = NULL, array(
$condition = NULL, $arguments = 'data' => t('Title'),
->where($snippet, $args = array())
->orderBy($field, $direction = array()) 'field' => 'n.title',
$snippet Where clause (with placeholders) 'sort' => 'desc'),
'ASC') ->rightJoin ($table, $alias = NULL,
$args Assoc array of placeholders and t('Operations'),
$field The field to order by. placeholder values. $condition = NULL, $arguments =
);
$direction 'ASC' or 'DESC'. array())
return The called query object. See join method.