You are here

function db_distinct_field in Drupal 6

Same name and namespace in other branches
  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()
  4. 5 includes/database.mysqli.inc \db_distinct_field()
  5. 5 includes/database.mysql.inc \db_distinct_field()
  6. 5 includes/database.pgsql.inc \db_distinct_field()

Adds the DISTINCT flag to the supplied query and returns the altered query.

The supplied query should not contain a DISTINCT flag. This will not, and never did guarantee that you will obtain distinct values of $table.$field.

Parameters

$table: Unused. Kept to retain API compatibility.

$field: Unused. Kept to retain API compatibility.

$query: Query to which the DISTINCT flag should be applied.

Return value

SQL query with the DISTINCT flag set.

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.inc, line 407
Wrapper for database interface code.

Code

function db_distinct_field($table, $field, $query) {
  $matches = array();
  if (!preg_match('/^SELECT\\s*DISTINCT/i', $query, $matches)) {

    // Only add distinct to the outer SELECT to avoid messing up subqueries.
    $query = preg_replace('/^SELECT/i', 'SELECT DISTINCT', $query);
  }
  return $query;
}