=== modified file 'sites/all/modules/weblinks/contribs/weblinks_blocks/weblinks_blocks.module'
--- sites/all/modules/weblinks/contribs/weblinks_blocks/weblinks_blocks.module	2009-07-14 16:07:47 +0000
+++ sites/all/modules/weblinks/contribs/weblinks_blocks/weblinks_blocks.module	2009-07-16 19:28:28 +0000
@@ -520,9 +520,9 @@
     case 'contributors':
       $rows = $header = array();
       $how_many = variable_get('weblinks_maxdisp_blocks_contributors', 10);
-      $result = db_query("SELECT u.uid, u.name, COUNT(n.uid) as count FROM `node` n "
-        ."LEFT JOIN `users` u USING(uid) WHERE n.type='weblinks' "
-        ."GROUP BY u.name ORDER BY count DESC", 0, $how_many);
+      $result = db_query("SELECT u.uid, u.name, COUNT(n.uid) as count FROM {node} n "
+        ."LEFT JOIN {users} u USING(uid) WHERE n.type='weblinks' "
+        ."GROUP BY u.name, u.uid ORDER BY count DESC", 0, $how_many);
       while ($row = db_fetch_object($result)) {
         $rows[] = array(
           theme('username', $row, array('levels' => FALSE, 'plain' => TRUE)),

=== modified file 'sites/all/modules/weblinks/weblinks.module'
--- sites/all/modules/weblinks/weblinks.module	2009-07-16 18:34:01 +0000
+++ sites/all/modules/weblinks/weblinks.module	2009-07-16 19:41:35 +0000
@@ -1056,7 +1056,7 @@
  * General database query function.
  *
  * @param $tid
- *   the term id to fetch links for.
+ *   the term id to fetch links for. May be a comma-separated list.
  * @param $sort
  *   the order in which to sort.
  * @param $limit
@@ -1065,73 +1065,97 @@
  * @return result set from the query.
  */
 function _weblinks_get_query($tid = 0, $sort = 'title', $limit = 0) {
-  $query = 'SELECT !cols FROM {node} n ';
-  $cols = array();
-  if ($sort === 'count') {
-    $cols[] = 'COUNT(n.nid)';
+  if ($tid === 'unpublished') {
+    $where = 'WHERE n.status = 0 ';
   }
   else {
-    $cols[] = 'DISTINCT(n.nid)';
+    $where = 'WHERE n.status = 1 ';
   }
-  $query .= 'INNER JOIN {node_revisions} nr ON nr.vid = n.vid ';
-  $query .= 'INNER JOIN {weblinks} bw ON bw.vid = nr.vid ';
+
+  $join = 'INNER JOIN {weblinks} bw ON n.vid = bw.vid '; // acts as filter on {node}
+  // In some cases we need a join on term_node; in some we don't. We decide later according to $sort
   if (function_exists('taxonomy_get_term')) {
-    $query .= 'LEFT JOIN {term_node} tn ON tn.nid=n.nid AND tn.vid=n.vid ';
-    $which_tid = array(0 => "AND tn.tid IN (%s) ", 1 => 'AND tn.tid IS NULL ');
-  }
-  else {
-    $which_tid = array('1=1 ');
-  }
-
-  if ($tid === 'unpublished') {
-    $query .= 'WHERE n.status = 0 ';
-  }
-  else {
-    $query .= 'WHERE n.status = 1 ';
-  }
-
+    if ($tid == 0) {
+      $join_tn = 'LEFT JOIN {term_node} tn ON tn.nid=n.nid AND tn.vid=n.vid ';
+      $where_tn = 'AND tn.nid IS NULL ';
+    }
+    else {
+      $join_tn = 'INNER JOIN {term_node} tn ON tn.nid=n.nid AND tn.vid=n.vid ';
+      $where_tn = 'AND tn.tid IN (%s) ';
+    }
+  }
+  else {
+    $join_tn = '';
+    $where_tn = '';
+  }
+
+  $groupby = FALSE;
+  $xgrpcols = '';
   switch ($sort) {
     case 'standard':
-      $query .= $which_tid[$tid == 0];
-      $query .= 'ORDER BY n.sticky DESC, n.created ASC';
-      $cols[] = 'n.sticky, n.created';
+      $cols = 'n.nid, n.sticky, n.created';
+      $join .= $join_tn;
+      $where .= $where_tn;
+//      $groupby = ($join_tn != '');
+      $order = 'ORDER BY n.sticky DESC, n.created ASC';
       break;
     case 'title':
-      $query .= $which_tid[$tid == 0];
-      $query .= 'ORDER BY n.sticky DESC, n.title ASC';
-      $cols[] = 'n.title';
+      $cols = 'n.nid, n.title';
+      $join .= $join_tn;
+      $where .= $where_tn;
+//      $groupby = ($join_tn != '');
+//      $xgrpcols = 'n.sticky'; // otherwise you can't order on it (at least not on pgsql)
+      $order .= 'ORDER BY n.sticky DESC, n.title ASC';
       break;
     case 'popular':
-      $query .= 'AND bw.click_count>0 ';
-      $query .= 'ORDER BY bw.click_count DESC, bw.last_click DESC';
-      $cols[] = 'bw.click_count, bw.last_click';
+      $cols = 'n.nid, bw.click_count, bw.last_click';
+      $where .= 'AND bw.click_count>0 ';
+      $order .= 'ORDER BY bw.click_count DESC, bw.last_click DESC';
       break;
     case 'user':
-      $query .= "AND n.uid=%s ";
-      $query .= 'ORDER BY n.sticky, n.created';
-      $cols[] = 'n.sticky, n.created';
+      $cols = 'n.nid, n.sticky, n.created';
+      $where .= 'AND n.uid=%s ';
+      $order .= 'ORDER BY n.sticky, n.created';
       break;
     case 'recent':
+      $cols = 'n.nid, n.changed, n.title';
       if ($tid != 0) {
-        $query .= $which_tid[0];
+        $join .= $join_tn;
+        $where .= $where_tn;
+//        $groupby = ($join_tn != '');
       }
-      $query .= 'ORDER BY n.changed DESC, n.title ASC';
-      $cols[] = 'n.changed, n.title';
+      $order .= 'ORDER BY n.changed DESC, n.title ASC';
       break;
     case 'random':
+      $cols = 'n.nid';
       if ($tid != 0) {
-        $query .= $which_tid[0];
+        $join .= $join_tn;
+        $where .= $where_tn;
       }
-      $query .= 'ORDER BY RAND()';
+      $order .= 'ORDER BY RAND()';
       break;
     case 'count':
+      $cols = 'COUNT(n.nid)';
       if ($tid !== 'unpublished') {
-        $query .= $which_tid[1];
+        $join .= $join_tn;
+        $where .= $where_tn;
       }
+      $order = '';
       break;
   }
-
-  $query = str_replace('!cols', implode(', ', $cols), $query);
+  
+  if ($groupby) {    
+    $groupby = 'GROUP BY ' . $cols;
+    if ($xgrpcols != '') {
+      $groupby .= ', ' . $xgrpcols;
+    }
+    $groupby .= ' ';
+  }
+  else {
+    $groupby = '';
+  }
+  
+  $query = 'SELECT ' . $cols . ' FROM {node} n ' . $join . $where . $groupby . $order;
   $query = db_rewrite_sql($query);
 // drupal_set_message("$tid, $sort: ".$query);
 

