db_distinct_field

  1. drupal
    1. 4.7 database.mysql.inc
    2. 4.7 database.mysqli.inc
    3. 4.7 database.pgsql.inc
    4. 5 database.mysqli.inc
    5. 5 database.mysql.inc
    6. 5 database.pgsql.inc
    7. 6
Versions
4.7 – 6 db_distinct_field($table, $field, $query)

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 function calls db_distinct_field()

db_rewrite_sql in drupal/includes/database.inc
Rewrites node, taxonomy and comment queries. Use it for listing queries. Do not use FROM table1, table2 syntax, use JOIN instead.

Code

drupal/includes/database.inc, line 407

<?php
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;
}
?>