diff --git a/ecrire/public/criteres.php b/ecrire/public/criteres.php
index 9a9e49e3e2cb41c1cfb64dbf9a663fb4899ff7ea..19476263ee8c288fefc0e56395159d17c3aeedc3 100644
--- a/ecrire/public/criteres.php
+++ b/ecrire/public/criteres.php
@@ -867,7 +867,7 @@ function calculer_jointure(&$boucle, $depart, $arrivee, $col='', $cond=false)
   // de l'index principal et de l'index de jointure (non conditionnel! [6031])
   // et operateur d'egalite (http://trac.rezo.net/trac/spip/ticket/477)
 
-  if ($pk = (count($boucle->from) == 1) && !$cond) {
+  if ($pk = (count($boucle->from) == 2) && !$cond) {
   	if ($pk = $a[1]['key']['PRIMARY KEY']) {
 		$id_primary = $ddesc['key']['PRIMARY KEY'];
 		$pk = preg_match("/^$id_primary, *$col$/", $pk) OR
diff --git a/ecrire/req/mysql.php b/ecrire/req/mysql.php
index 26259a51091fd13d6fbe1930cfd34975ff7fdc3a..412a2401d81ac138381eec3e38b0c7307d0b1bc8 100644
--- a/ecrire/req/mysql.php
+++ b/ecrire/req/mysql.php
@@ -219,7 +219,8 @@ function spip_select_as($args)
 {
 	$argsas = "";
 	foreach($args as $k => $v) {
-		$argsas .= ', ' . $v . (is_numeric($k) ? '' : " AS `$k`");
+		if (strpos($v, 'JOIN') === false)  $argsas .= ', ';
+		$argsas .= $v . (is_numeric($k) ? '' : " AS `$k`");
 	}
 	return substr($argsas,2);
 }
diff --git a/ecrire/req/pg.php b/ecrire/req/pg.php
index 852c5c5a7c6da27037f48df79d86f8864cd3414e..769f70b7b2f2e4aa4a7451b5bf0808239c32bb48 100644
--- a/ecrire/req/pg.php
+++ b/ecrire/req/pg.php
@@ -329,7 +329,7 @@ function spip_pg_orderby($order, $select)
 function spip_pg_groupby($groupby, $from, $select)
 {
 	$join = is_array($from) ? (count($from) > 1) : 
-	  (strpos($from, ",") OR strpos($from, "JOIN"));
+	  (strpos($from, ","));
 	if ($join OR $groupby) $join = !is_array($select) ? $select : join(", ", $select);
 	if ($join) {
 	  $join = str_replace('DISTINCT ','',$join);
@@ -368,13 +368,17 @@ function spip_pg_frommysql($arg)
 
 	$res = spip_pg_fromfield($arg);
 
-	$res = preg_replace('/\brand[(][)]/','random()', $res);
+	$res = preg_replace('/\brand[(][)]/i','random()', $res);
+
 	$res = preg_replace('/\b0\.0[+]([a-zA-Z0-9_.]+)\s*/',
 			    'CAST(substring(\1, \'^ *[0-9.]+\') as float)',
 			    $res);
 	$res = preg_replace('/\b0[+]([a-zA-Z0-9_.]+)\s*/',
 			    'CAST(substring(\1, \'^ *[0-9]+\') as int)',
 			    $res);
+	$res = preg_replace('/\bconv[(]([^,]*)[^)]*[)]/i',
+			    'CAST(substring(\1, \'^ *[0-9]+\') as int)',
+			    $res);
 	$res = preg_replace('/UNIX_TIMESTAMP\s*[(]\s*[)]/',
 			    'EXTRACT(\'epoch\' FROM NOW())', $res);
 
@@ -463,7 +467,8 @@ function spip_pg_select_as($args)
 				$v = $k;
 			elseif ($v != $k) $as = " AS $k"; 
 		}
-		$argsas .= ', ' . $v . $as; 
+		if (strpos($v, 'JOIN') === false)  $argsas .= ', ';
+		$argsas .= $v . $as; 
 	}
 	return substr($argsas,2);
 }