如何整合COUNT(*)?

时间:2011-11-26 15:00:55

标签: php mysql

<?php
    $dbname = 'pdartist2';
    $table = 'artwork';
    $result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder from artwork where SCID = $SCID order by DisplayOrder") or die(mysql_error());
while($row = mysql_fetch_array($result)){   
    $AID = $row['AID'];
    $ThumbFilePath = $row['ThumbFilePath'];
    $Title = $row['Title'];
    $DisplayOrder = $row['DisplayOrder'];
    $RecordCount = $row['Count']; {
            $ColCount = $ColCount+1;
        if ($ColCount ==5) {
            echo "<div id='clear'></div>";
            $ColCount = 1;
    }
    echo "<div id='thumb' onClick='window.location='gallery_detail.php?AID=AID';' title='Enlarge'>";
    echo "<div id='thumb_container'>";
    echo "<a href='gallery_detail.php?AID=$AID'><img src='/$ThumbFilePath' title='Enlarge' alt='Enlarge' border='0'></a>";
    echo "<div id='name_spacer'>";
    ($ColCount);
    echo "<div id='thumbdesc'>$Title</div>";
    echo "</div>";
    echo "</div>";
    echo "</div>";
    }       
}
    mysql_free_result($result);
    ?>

当我运行此查询时,它可以正常工作。添加COUNT()时会出现问题。我需要这样的东西:

$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder, COUNT(*) AS RecordCount from artwork  where SCID = $SCID order by DisplayOrder") or die(mysql_error());   

但该查询的结果并非应有的结果。

4 个答案:

答案 0 :(得分:4)

$query = "select AID, ThumbFilePath, Title, DisplayOrder 
          from artwork where SCID = $SCID order by DisplayOrder";
$result = mysql_query($query) or trigger_error(mysql_error()); 
$scid_count = mysql_num_rows($result);

是你在寻找什么?

答案 1 :(得分:2)

我认为您希望拥有总行数,但COUNT(*)会自动将它们组合在一起。

考虑使用子查询。

$result = mysql_query("select AID, ThumbFilePath, Title, DisplayOrder, (SELECT COUNT(*) FROM artwork WHERE SCID=$SCID) ResultCount FROM artwork  where SCID = $SCID order by DisplayOrder") or die(mysql_error());

答案 2 :(得分:1)

当您添加COUNT(*)时,您将返回所有行的计数,这会导致单行返回。如果您想要从原始查询返回的总行数,请在查询返回后使用mysql_num_rows。

答案 3 :(得分:0)

这有用吗:

select AID, ThumbFilePath, Title, DisplayOrder, COUNT(*) AS RecordCount from artwork  where SCID = $SCID order by DisplayOrder group by AID