【问题标题】:'Counting' the number of records that match a certain criteria and displaying the numbers (using PHP and MySQL)“计数”符合特定条件的记录数并显示数字(使用 PHP 和 MySQL)
【发布时间】:2011-03-25 13:54:22
【问题描述】:

我有一个 MySQL 数据库,其中包含用户所在国家/地区以及他们是个人还是组织。字段名称为“国家”和“类型”。

使用 PHP,我想“统计”数据库中的国家数量、个人数量和组织数量,然后以以下示例格式显示这些数字:

<p>So far, <strong>500</strong> individuals and <strong>210</strong> organisations from <strong>40</strong> countries have registered their support.</p>

如果有帮助,我目前正在使用以下代码列出记录总数:

<?php
    $link = mysql_connect("localhost", "username", "password");
    mysql_select_db("database_name", $link);        
    $result = mysql_query("SELECT * FROM table_name", $link);
    $num_rows = mysql_num_rows($result);        
    echo "&nbsp;$num_rows\n ";  
?>

我的 PHP / MySQL 技能非常有限,所以我真的在这方面苦苦挣扎。

非常感谢!

【问题讨论】:

  • 你已经拥有了什么(php / sql)?
  • 获取这些数字需要你的mysql技能
  • 我目前只列出数据库中的记录总数,恐怕就我的PHP技能而言:$result = mysql_query("SELECT * FROM database", $link ); $num_rows = mysql_num_rows($result); echo " $num_rows\n ";
  • 啊,是的。抱歉,我的意思是说 PHP / MySQL 技能。
  • 我已经更新了我的初始查询以包含我当前用来显示所有记录数的代码。

标签: php mysql


【解决方案1】:

要获得国家的数量:

SELECT COUNT(DISTINCT country) AS NumCountries FROM tableName

要获取个人数量或组织数量:

SELECT COUNT(*) AS NumIndividuals FROM tableName WHERE type = 'individual'
SELECT COUNT(*) AS NumOrganisations FROM tableName WHERE type = 'organisation'

【讨论】:

  • 非常感谢。正如 Cassy 建议的那样,我将尝试与他提供的代码一起实现这一点。
【解决方案2】:

您要查找的是基于分组的计数。试试这样的:

$sql = "SELECT type, count(*) as cnt FROM users GROUP BY type";
$result = mysql_query($sql);

$counts = array();

while ($row = mysql_fetch_assoc($result)) {
    $counts[$row['type']] = $row['cnt'];
}

这会给你一个类似的数组

Array (
    'individual' => 500,
    'organization' => 210
)

要计算国家/地区,请使用第一条语句posted by Hammerite


编辑:添加了一个详细的国家统计示例

$sql = "SELECT COUNT(DISTINCT country) AS NumCountries FROM users";
$result = mysql_query($sql);

$number_of_countries = 0;
if ($row = mysql_fetch_assoc($result)) {
    $number_of_countries = $row['NumCountries'];
}

然后你就可以打印出来了:

printf('<p>So far, <strong>%d</strong> individuals and <strong>%d</strong> '.
       'organisations from <strong>%d</strong> countries have registered '.
       'their support.</p>', $counts['individual'], $counts['organization'],
        $number_of_countries);

【讨论】:

  • 哇,非常详细。非常感谢。我将按照您的建议尝试执行 Hammerite 发布的声明。
  • 你好,凯西。感谢您提供的这段代码,我非常接近!我已经设法输出了个人和组织的数量,但我正在与这些国家作斗争。我只是不确定在哪里放置 Hammerite 的 MySQL 语句以及如何将它与 printf 语句中的 '$number_of_countries' 变量链接起来。你能帮忙吗?
  • 是的,是的,是的!非常感谢您的所有帮助。这绝对完美。如果我能投票就好了。
【解决方案3】:

答案是使用SQL中的COUNT(*)函数来检索答案:

SELECT COUNT(*) AS individual_count FROM user WHERE type = 'individual';
SELECT COUNT(*) AS organization_count FROM user WHERE type = 'organization';
SELECT COUNT(*) AS country_count FROM user GROUP BY country;

最后一个将按国家/地区名称对您的查询集进行分组,并为每个国家/地区生成一行。在这个结果集上使用 COUNT 将给出不同国家的计数。

然后,您可以通过对来自 mysql_query 的 $result 使用 mysql_fetch_assoc 来获取此值,并且答案将包含在每个查询的 'invididual_count'、'organization_count' 和 'country_count' 中。

【讨论】:

  • 这也很有用,谢谢。有了所有已提供的答案,我将尽我所能立即完成这项工作!
【解决方案4】:

感谢您为此提供的所有帮助(尤其是 Cassy)。

我认为显示完整代码是值得的,以防其他人将来遇到类似要求:

<?php
$link = mysql_connect("localhost", "username", "password");
mysql_select_db("database_name", $link);

$sql = "SELECT type, COUNT(*) as cnt FROM table_name GROUP BY type";
$result = mysql_query($sql);
$counts = array();
while ($row = mysql_fetch_assoc($result)) {
    $counts[$row['type']] = $row['cnt'];
}

$sql = "SELECT COUNT(DISTINCT country) AS NumCountries FROM table_name";
$result = mysql_query($sql);                
$number_of_countries = 0;
if ($row = mysql_fetch_assoc($result)) {
    $number_of_countries = $row['NumCountries'];
}

printf('<p><strong>So far, <em class="count">%d</em> individuals and <em class="count">%d</em> organisations from <em class="count">%d</em> countries have registered their support.', $counts['Individual'], $counts['Organisation'], $number_of_countries); ?>

【讨论】:

    【解决方案5】:

    如果您只是在寻找返回的行数,试试这个:

    $result = mysql_db_query($db, $query);
    $num_rows = mysql_num_rows($result);
    

    另一种选择是使用 mysql 计数函数执行单独的查询并使用其中的结果。

    【讨论】:

    • 不,他不是在寻找返回的行数。他只找一排
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多