简体   繁体   English

mysql计数用join和where子句降序,两个表,限制?

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

Here i have implemented two tables (pages, pagestatistics).I need result for last five records using join from the those tables and also i need a count of cID field from second table (pagestatistics), i need that count result will be display in descending. 在这里,我实现了两个表(页面,页面统计)。我需要使用这些表中的连接获取最后五个记录的结果,并且我需要第二个表中的cID字段的计数(页面统计),我需要将计数结果显示在下降。

NOTE : Primary id is cID. 注意:主要ID为cID。

![pagestatistics][1]![pages][2] ![pagestatistics] [1]![pages] [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 }?>

How to display all records in descending order. 如何以降序显示所有记录。 Thank in advance. 预先感谢。

You can do it in this way just order by with your count separated with , in ORDER BY clause 您可以通过这种方式来执行此操作,只需在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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM