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

时间:2013-07-10 11:25:04

标签: php mysql sql mysqli

这里我已经实现了两个表(页面,页面统计)。我需要使用来自这些表的连接的最后五个记录的结果,并且我还需要从第二个表(pagestatistics)计算cID字段,我需要计数结果将以降序显示。

注意:主要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 }?>

如何按降序显示所有记录。 预先感谢。

1 个答案:

答案 0 :(得分:0)

您可以通过这种方式执行此操作,只需将您的count, ORDER BY段中的$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"); 分开订购

{{1}}