--- includes/database.mysql.inc.orig	2009-07-21 04:52:29.000000000 -0400
+++ includes/database.mysql.inc	2009-09-19 18:21:44.047168479 -0400
@@ -365,7 +365,7 @@ 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',
+      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*,|\s+AS|\s*$)/is',
       '\1'. $field_to_select .'\2', $matches[1], 1
     );
     
--- includes/database.mysqli.inc.orig	2009-07-21 04:52:30.000000000 -0400
+++ includes/database.mysqli.inc	2009-09-19 18:21:56.124886580 -0400
@@ -367,7 +367,7 @@ 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',
+      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*,|\s+AS|\s*$)/is',
       '\1'. $field_to_select .'\2', $matches[1], 1
     );
     
