You are here

function db_distinct_field in Drupal 4

Same name in this branch
  1. 4 includes/database.mysqli.inc \db_distinct_field()
  2. 4 includes/database.mysql.inc \db_distinct_field()
  3. 4 includes/database.pgsql.inc \db_distinct_field()
Same name and namespace in other branches
  1. 5 includes/database.mysqli.inc \db_distinct_field()
  2. 5 includes/database.mysql.inc \db_distinct_field()
  3. 5 includes/database.pgsql.inc \db_distinct_field()
  4. 6 includes/database.inc \db_distinct_field()

Wraps the given table.field entry with a DISTINCT(). The wrapper is added to the SELECT list entry of the given query and the resulting query is returned. This function only applies the wrapper if a DISTINCT doesn't already exist in the query.

Parameters

$table Table containing the field to set as DISTINCT:

$field Field to set as DISTINCT:

$query Query to apply the wrapper to:

Return value

SQL query with the DISTINCT wrapper surrounding the given table.field.

Related topics

1 call to db_distinct_field()
db_rewrite_sql in includes/database.inc
Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.

File

includes/database.pgsql.inc, line 375
Database interface code for PostgreSQL database servers.

Code

function db_distinct_field($table, $field, $query) {
  $field_to_select = 'DISTINCT ON (' . $table . '.' . $field . ") {$table}.{$field}";

  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
  $query = preg_replace('/(SELECT.*)(' . $table . '\\.)?(?<!DISTINCT\\()(?<!DISTINCT\\(' . $table . '\\.)' . $field . '(.*FROM )/AUsi', '\\1' . $field_to_select . '\\3', $query);
  $query = preg_replace('/(ORDER BY )(?!' . $table . '\\.' . $field . ')/', '\\1' . "{$table}.{$field}, ", $query);
  return $query;
}