获取大量数据,然后从中进行计数


fetching large data and then counting from them

我想知道如果我从MySQL的所有不同表中获取大量数据,那么我如何计算特定行的值总数?

$thedata= mysql_query("select comment,Date,hits,abc,xyz from fk_views where onid='$thepicid'");

我想统计此查询获取的总命中数。通常我们确实喜欢

mysql_num_rows($thedata);

此查询将显示提取的数据总数,而不是提取的命中总数。

正如@Barmar所建议的,您可能想要使用SUM,但是字段hits本身不是正返回您想要的吗?这一列是如何增长的?

你可以试试这个:

$thedata= mysqli_query("select SUM(hits) as hitcount,comment,Date,hits,abc,xyz from fk_views where onid='$thepicid'");

这将在同一个查询中获得总命中率。

<?php
// Create connection
$con=mysqli_connect("localhost","user","password","database");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$thedata= mysqli_query($con, "select comment,Date,hits,abc,xyz from fk_views where onid='$thepicid'");
$row_count = mysqli_num_rows($thedata);
echo $row_count;
?>