--- database.mysql.inc	2009-10-21 23:37:31.000000000 +0200
+++ database.mysql.inc.new	2009-10-21 23:35:21.000000000 +0200
@@ -365,8 +365,8 @@ function db_distinct_field($table, $fiel
   $matches = array();
   if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
     $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
-      '\1'. $field_to_select .'\2', $matches[1], 1
+      '/((?:^|,|\()\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|\sAS|\)|$))/is',
+      '\1'. $field_to_select .'\2', $matches[1]
     );
     
     return 'SELECT'. $select .'FROM'. $matches[2];
--- database.mysqli.inc	2009-10-21 23:40:24.000000000 +0200
+++ database.mysqli.inc.new	2009-10-21 23:35:12.000000000 +0200
@@ -367,8 +367,8 @@ function db_distinct_field($table, $fiel
   $matches = array();
   if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
     $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
-      '\1'. $field_to_select .'\2', $matches[1], 1
+      '/((?:^|,|\()\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|\sAS|\)|$))/is',
+      '\1'. $field_to_select .'\2', $matches[1]
     );
     
     return 'SELECT'. $select .'FROM'.$matches[2];
