db_distinct_field

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

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

Code

includes/database.mysqli.inc, line 420

<?php
function db_distinct_field($table, $field, $query) {
  $field_to_select = 'DISTINCT(' . $table . '.' . $field . ')';
  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
  return preg_replace('/(SELECT.*)(?:' . $table . '\.|\s)(?<!DISTINCT\()(?<!DISTINCT\(' . $table . '\.)' . $field . '(.*FROM )/AUsi', '\1 ' . $field_to_select . '\2', $query);
}
?>