Mysql计数采用降序连接和where子句,两个表,限制


Mysql count with descending with join and where clause ,two tables, limit?

这里我实现了两个表(pages,pagestatistics)。我需要使用这些表中的联接的最后五条记录的结果,还需要第二个表中的cID字段的计数(pagestatistics),我需要计数结果以降序显示。

注意:主id是cID。

[pagestatistics][1]![页][2]

   <?php 
$recentpageviews =mysql_query("SELECT  Distinct(cID) FROM `pagestatistics`  order by `pstID` desc limit 0,5");
$downloads=mysql_num_rows($recentpageviews);
$k=1;
$cid="";
      while($views_values=mysql_fetch_array($recentpageviews))
{       
        $cid.=",".$views_values['cID'];
$k++;}
}
$explode =explode(",",$cid);
for($i=1;$i<count($explode);$i++)
{
$sql=mysql_query("select  Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date from pagestatistics as a left join pages as b on a.cID=b.cID where a.cID='".$explode[$i]."' order by a.cID desc");
$res=mysql_fetch_array($sql);
?>
 <tr>
        <td class='ccm-site-statistics-downloads-title'><?php echo $res['cFilename'];?></td>
        <td class='ccm-site-statistics-downloads-title'><?php echo $res['clicks'];?></td>
       <td class='ccm-site-statistics-downloads-title'><?php echo $res['date'];?></td>
    </tr>
<?php }?>

如何按降序显示所有记录。提前感谢。

您可以用这种方式完成,只需在ORDER BY子句中用,分隔count即可

$sql=mysql_query("select  Distinct(a.cID),count(a.cID) as clicks,b.cFilename,a.date
 from pagestatistics as a left join pages as b on a.cID=b.cID 
where a.cID='".$explode[$i]."' order by a.cID,clicks desc");