public function SelectQuery::addExpression

7.x public SelectQuery::addExpression($expression, $alias = NULL, $arguments = array())

Adds an expression to the list of "fields" to be SELECTed.

An expression can be any arbitrary string that is valid SQL. That includes various functions, which may in some cases be database-dependent. This method makes no effort to correct for database-specific functions.


$expression: The expression string. May contain placeholders.

$alias: The alias for this expression. If not specified, one will be generated automatically in the form "expression_#". The alias will be checked for uniqueness, so the requested alias may not be the alias that is assigned in all cases.

$arguments: Any placeholder arguments needed for this expression.

Return value

The unique alias that was assigned for this expression.

Overrides SelectQueryInterface::addExpression

2 calls to SelectQuery::addExpression()
SelectQuery::orderRandom in drupal/includes/database/
Orders the result set by a random value.
SelectQuery_pgsql::orderRandom in drupal/includes/database/pgsql/
Orders the result set by a random value.


drupal/includes/database/, line 1316


Query builder for SELECT statements.


public function addExpression($expression, $alias = NULL, $arguments = array()) {
  if (empty($alias)) {
    $alias = 'expression';

  $alias_candidate = $alias;
  $count = 2;
  while (!empty($this->expressions[$alias_candidate])) {
    $alias_candidate = $alias . '_' . $count++;
  $alias = $alias_candidate;

  $this->expressions[$alias] = array(
    'expression' => $expression,
    'alias' => $alias,
    'arguments' => $arguments,

  return $alias;