簡體   English   中英

mysql計數用join和where子句降序,兩個表,限制?

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

在這里,我實現了兩個表(頁面,頁面統計)。我需要使用這些表中的連接獲取最后五個記錄的結果,並且我需要第二個表中的cID字段的計數(頁面統計),我需要將計數結果顯示在下降。

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

如何以降序顯示所有記錄。 預先感謝。

您可以通過這種方式來執行此操作,只需在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