| 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.
$table Unused. Kept to retain API compatibility.
$field Unused. Kept to retain API compatibility.
$query Query to which the DISTINCT flag should be applied.
SQL query with the DISTINCT flag set.
drupal/
<?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;
}
?>