'计数'符合特定条件并显示数字的记录数(使用PHP和MySQL)

时间:2011-03-25 13:54:22

标签: php mysql

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

使用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技能非常有限,所以我真的很挣这个。

非常感谢提前!

5 个答案:

答案 0 :(得分:4)

获取国家数量:

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'

答案 1 :(得分:3)

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

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

答案 2 :(得分:2)

答案是使用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”中。

答案 3 :(得分:0)

感谢您对此的所有帮助(尤其是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); ?>

答案 4 :(得分:-1)

如果您只想查找返回的行数,请尝试以下方法:

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

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