--- /home/jcv/database.inc	2010-08-14 01:21:20.086790703 +0100
+++ includes/database/mysql/database.inc	2010-08-14 01:26:13.833922426 +0100
@@ -62,7 +62,19 @@
     // kinds of database systems. These settings force MySQL to behave
     // the same as postgresql, or sqlite in regards to syntax interpretation
     // and invalid data handling. See http://drupal.org/node/344575 for further discussion.
-    $this->exec("SET sql_mode='ANSI,TRADITIONAL'");
+    // MySQL has a problem ONLY_FULL_GROUP_BY mode in some versions. See 
+    // http://bugs.mysql.com/bug.php?id=39656. Known to cause problems between
+    // 5.0.67 (TBC) and 5.0.74 and between 5.1.28 (TBC) and 5.1.31
+    // @todo: This override should be removed once we require a version of MySQL newer than 5.1.31
+    $mysql_ver = $this->version();
+    $full_group_by_ok = (version_compare($mysql_ver, '5.0.67', '<') || version_compare($mysql_ver, '5.0.74', '>=')) &&
+                        (version_compare($mysql_ver, '5.1.28', '<') || version_compare($mysql_ver, '5.1.31', '>='));
+    if ($full_group_by_ok) {
+      $this->exec("SET sql_mode='ANSI,TRADITIONAL,ONLY_FULL_GROUP_BY'");
+    }
+    else {
+      $this->exec("SET sql_mode='ANSI,TRADITIONAL'");
+    }
   }
 
   public function queryRange($query, $from, $count, array $args = array(), array $options = array()) {
