简体   繁体   中英

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.

NOTE : Primary id is cID.

![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

$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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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