Index: views.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/views.module,v
retrieving revision 1.166.2.43
diff -u -u -p -r1.166.2.43 views.module
--- views.module	14 Jul 2007 18:54:16 -0000	1.166.2.43
+++ views.module	22 Oct 2007 20:08:09 -0000
@@ -565,11 +565,13 @@ function views_build_view($type, &$view,
   }
 
   $query = db_rewrite_sql($info['query'], 'node');
+  $query = str_replace('[SUBQUERY]', $info['subquery'], $query);
 
   $items = array();
   if ($query) {
     if ($view->use_pager) {
       $cquery = db_rewrite_sql($info['countquery'], 'node', 'nid', $info['rewrite_args']);
+      $cquery = str_replace('[SUBQUERY]', $info['subquery'], $cquery);
       $result = pager_query($query, $view->pager_limit, $view->use_pager - 1, $cquery, $info['args']);
       $view->total_rows = $GLOBALS['pager_total_items'][$view->use_pager - 1];
     }
@@ -704,6 +706,20 @@ function _views_get_timezone() {
   return $timezone;
 }
 
+/*
+ * Determine whether or not the database supports sub-queries.
+ */
+function _views_is_subquery() {
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      return version_compare(mysql_get_server_info(), '4.1', '>=');
+
+    case 'pgsql':
+      return TRUE;
+  }
+}
+
 /**
  * Figure out what the URL of the view we're currently looking at is.
  */
@@ -2143,4 +2159,4 @@ function views_form_alter($form_id, &$fo
 // An implementation of hook_devel_caches() from devel.module. Must be in views.module so it always is included.
 function views_devel_caches() {
   return array('cache_views');
-}
\ No newline at end of file
+}
Index: views_cache.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/Attic/views_cache.inc,v
retrieving revision 1.2.2.18
diff -u -u -p -r1.2.2.18 views_cache.inc
--- views_cache.inc	14 Jul 2007 19:12:02 -0000	1.2.2.18
+++ views_cache.inc	22 Oct 2007 20:08:09 -0000
@@ -276,9 +276,11 @@ function _views_get_query(&$view, $args,
   else {
     views_load_query();
     $info = _views_build_query($view, $args, $filters);
+    $query = str_replace('[SUBQUERY]', $info['subquery'], $info['query']);
+    $countquery = str_replace('[SUBQUERY]', $info['subquery'], $info['countquery']);
     $data = array(
-        'query' => _views_replace_args($info['query'], $info['args']),
-        'countquery' => _views_replace_args($info['countquery'], $info['args']),
+      'query' => _views_replace_args($query, $info['args']),
+      'countquery' => _views_replace_args($countquery, $info['args']),
     );
     if ($view->is_cacheable) {
       cache_set('views_query:' . $view->name, 'cache_views', serialize($data));
@@ -332,4 +334,4 @@ function _views_get_style_plugins($title
     }
   }
   return ($titles ? $views_style_plugins['title'] : $views_style_plugins['base']);
-}
\ No newline at end of file
+}
Index: views_query.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/views/Attic/views_query.inc,v
retrieving revision 1.51.2.11
diff -u -u -p -r1.51.2.11 views_query.inc
--- views_query.inc	12 Apr 2007 15:18:59 -0000	1.51.2.11
+++ views_query.inc	22 Oct 2007 20:08:09 -0000
@@ -63,9 +63,10 @@ function _views_build_query(&$view, $arg
 
   $info['query'] = $query->query();
   $info['countquery'] = $query->query(true);
+  $info['subquery'] = $query->subquery();
   $info['summary'] = $summary;
   $info['level'] = $level;
-  $info['args'] = $query->where_args;
+  $info['args'] = array_merge($query->subquery_args, $query->where_args);
 
   return $info;
 
@@ -289,11 +290,13 @@ class _views_query {
     $this->primary_table = $primary_table;
     $this->primary_field = $primary_field;
     $this->joins = array();
+    $this->subquery = array();
     $this->where = array();
     $this->orderby = array();
     $this->groupby = array();
     $this->tables = array();
     $this->where_args = array();
+    $this->subquery_args = array();
     $this->use_alias_prefix = $alias_prefix;
     // Joins care about order, so we put our tables in a queue to make sure
     // the order is correct.
@@ -443,6 +446,31 @@ class _views_query {
   }
 
   /*
+   * Add a subquery as a table join to the query.
+   *
+   * @param $query
+   *   The SQL of the subquery to add.
+   * @param $args
+   *   The subquery's replacable arguments
+   * @param $joininfo
+   *   specify how to join the table
+   * @param $alias
+   *   the alias name associated with the subquery
+   */
+  function add_subquery($query, $args, $joininfo, $alias) {
+    if (_views_is_subquery()) {
+      $this->subquery[$alias] = $query;
+      $this->joins[$alias][0] = $joininfo;
+      $this->subquery_args = array_merge($this->subquery_args, $args);
+    }
+    else {
+      if (db_query_temporary($query, $args, $alias)) {
+        $this->add_table($alias, FALSE, 1, $join);
+      }
+    }
+  }
+
+  /*
    * This function will add a table to the query.
    *
    * @param $table
@@ -580,6 +608,9 @@ class _views_query {
       $this->no_distinct = TRUE;
     }
 
+    // Add a placeholder for sub-queries
+    $joins = count($this->subquery) ? '[SUBQUERY]' : '';
+
     // Add all the tables to the query via joins. We assume all LEFT joins.
     foreach ($this->tablequeue as $tinfo) {
       $table = $tinfo['table'];
@@ -658,6 +689,26 @@ class _views_query {
 
     return $query;
   }
+
+  /*
+   * Return the join clauses for all of the sub-queries.
+   * Do this here instead of in query() because db_rewrite_sql() improperly
+   * rewrites sub-queries.  See http://drupal.org/node/151910
+   */
+  function subquery() {
+    // Add the subqueries to the query via joins.
+    foreach ($this->subquery as $alias => $query) {
+      $joininfo = $this->joins[$alias][0];
+
+      $left_table_alias = isset($joininfo['left']['alias']) ? $joininfo['left']['alias'] : $tinfo['alias_prefix'];
+      $left_table_alias .= $joininfo['left']['table'];
+
+      $join_type = $joininfo['type'] == 'inner' ? 'INNER' : 'LEFT';
+      $joins .= " $join_type JOIN (" . $query . ") $alias ON " . $left_table_alias .".".
+        $joininfo['left']['field'] . " = $alias." . $joininfo['right']['field'];
+    }
+    return $joins;
+  }
 }
 
 /*
