If you're working with Drupal 6.14, you'll want to be aware of http://drupal.org/node/284392 (db_rewrite_sql causing issues with DISTINCT), a significant regression.

On Tue, Nov 24, 2009 at 4:03 AM, Lluís <enboig@gmail.com> wrote:
I am using group by module, and db_rewrite_sql is giving me problems
because it cause rows to be selected twice and SUM reports double
their value.

After doing some research I have found that if I change:
function node_db_rewrite_sql($query, $primary_table, $primary_field) {
 if ($primary_field == 'nid' && !node_access_view_all_nodes()) {
   $return['join'] = _node_access_join_sql($primary_table);
   $return['where'] = _node_access_where_sql();
   $return['distinct'] = 1;
   return $return;
 }
}

to:
function node_db_rewrite_sql($query, $primary_table, $primary_field) {
 if ($primary_field == 'nid' && !node_access_view_all_nodes()) {
   if (($where=_node_access_where_sql('view','new_na'))!='') {
      $return['where'] = "".$primary_table.".".$primary_field." IN
(SELECT new_na.nid FROM {node_access} new_na WHERE ".$where.")";
      return $return;
   }
 }
}

everything works as expected.

Is there any reason why using "INNER JOIN" is better than using "
WHERE nid IN (SELECT ... FROM {node_access} ... )"?

--
*Les normes hi són perquè hi pensis abans de saltar-te-les
*La vida és com una taronja, què esperes a exprimir-la?
*Si creus que l'educació és cara, prova la ignorància.
*La vida és com una moneda, la pots gastar en el que vulguis però
només una vegada.
*Abans d'imprimir aquest missatge, pensa en el medi ambient.



--
Randy Fay
Drupal Development, troubleshooting, and debugging
randy@randyfay.com
+1  970.462.7450