diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc
index 43342e9..a06bd17 100644
--- a/includes/database.mysql.inc
+++ b/includes/database.mysql.inc
@@ -365,7 +365,7 @@ function db_distinct_field($table, $field, $query) {
   $matches = array();
   if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
     $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
+      '/((?:^|,|\()\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|\sAS|\)|$))/is',
       '\1'. $field_to_select .'\2', $matches[1], 1
     );
     
diff --git a/includes/database.mysqli.inc b/includes/database.mysqli.inc
index a2415a1..aeb980c 100644
--- a/includes/database.mysqli.inc
+++ b/includes/database.mysqli.inc
@@ -367,7 +367,7 @@ function db_distinct_field($table, $field, $query) {
   $matches = array();
   if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) {
     $select = preg_replace(
-      '/((?:^|,)\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|$))/is',
+      '/((?:^|,|\()\s*)(?<!DISTINCT\()(?:'. $table .'\.)?'. $field .'(\s*(?:,|\sAS|\)|$))/is',
       '\1'. $field_to_select .'\2', $matches[1], 1
     );
     
diff --git a/modules/system/tests/database.test b/modules/system/tests/database.test
new file mode 100644
index 0000000..3da8409
--- /dev/null
+++ b/modules/system/tests/database.test
@@ -0,0 +1,110 @@
+<?php
+// $Id:  $
+/**
+ * @file
+ * Tests for testing the database abstraction layer in Drupal 6.
+ */
+
+class DatabaseTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  public static function getInfo() {
+    return array(
+      'name' => t('Database functionality'),
+      'description' => t('Exercise the database helper functions, this not not test the database but the functions that abstract the database.'),
+      'group' => t('Database'),
+    );
+  }
+
+
+  /**
+   * Test the db_distinct_field() function.
+   *
+   * See http://drupal.org/node/284392 for more information
+   */
+  function testDbDistinctField() {
+
+    // These assertions are taken from:
+    // http://drupal.org/node/284392
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT table.field FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT table.field AS table_field FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT DISTINCT(table.field) FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT table.field,table.field2 FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT table.field AS table_field, table.field2 AS table_field2 FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT DISTINCT(table.field),table.field2 FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table"),
+      'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT COUNT(table.field) FROM table"),
+      'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT COUNT(table.field) AS table_field FROM table"),
+      'expected' => 'SELECT COUNT(DISTINCT(table.field)) AS table_field FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT table.field1, COUNT(table.field), table.field2 FROM table"),
+      'expected' => 'SELECT table.field1, COUNT(DISTINCT(table.field)), table.field2 FROM table',
+    );
+
+    $assertions[] = array(
+      'input' => array("table", "field", "SELECT COUNT(DISTINCT(table.field)) FROM table"),
+      'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table',
+    );
+
+
+    foreach ($assertions as $assert) {
+      $distinct_added = call_user_func_array('db_distinct_field', $assert['input']);
+      if (!$this->assertEqual(
+        $distinct_added,
+        $assert['expected'],
+        t('Add DISTINCT to %query', array('%query' => $assert['input'][2]))
+      )) {
+        $this->pass(t('Query was rewritten to: %query, expected query: %expected_query', array('%query' => $distinct_added, '%expected_query' => $assert['expected'])));
+      }
+    }
+
+
+
+
+
+  }
+
+
+}
