2

我正在尝试编写一个脚本,该脚本从 SQL 查询中获取结果,并在遍历每个结果时检查 $row[13] 的名称应该是什么,然后我想要遍历它识别的每个查询结果任何相似的名称,并记录它看到该名称的次数。我以为我有一个循环计算出相同名称在数组中出现的次数,然后显示它。我正在寻找的最终结果是

Operations - 87
Training - 32
HR - 12

但是在我运行这段代码之后:

      while($row = mysql_fetch_array($result_id))
  {
        $profile8 = mysql_query
                    ("
                        SELECT org
                        FROM cost_centers
                        WHERE cost_center = '$row[13]'
                    ")
                    or die(mysql_error());
                    $anymatches=mysql_num_rows($profile8);
                        while($pro8 = mysql_fetch_array($profile8))
                        {
                            $orgnames = $pro8[0];
                        }


                            $names[] = $orgnames;

                            $newArray = array_count_values($names);

                            foreach ($newArray as $key => $value) {
                                echo "$key - $value <br />";
                            }

  }

我最终会像这样不断地计算它:

HR - 1 
HR - 1 
Communications - 1 
HR - 1 
Communications - 1 
- 1 
HR - 1 
Communications - 1 
- 2 
HR - 1 
Communications - 1 
- 3 
HR - 1 
Communications - 1 
- 4 
HR - 2 
Communications - 1 
- 4 
HR - 3 
Communications - 1 
- 4 
HR - 3 
Communications - 1 
- 4 
Operations - 1 
HR - 4 
Communications - 1 
- 4 
Operations - 1 
HR - 4 
Communications - 1 
- 5 
Operations - 1 
HR - 4 
Communications - 1 
- 6 
Operations - 1 
HR - 4 
Communications - 1 
- 6 
Operations - 2 
HR - 4 
Communications - 1 
- 6 
Operations - 3 
HR - 4 

所以它看起来就像是先分组然后计数,然后在不断增加计数时重新分组。我希望这是有道理的,并提前感谢。

4

4 回答 4

1

为什么不使用以下查询:

SELECT 
    SUM(IF(org='HR', 1, 0)) AS HRCount,
    SUM(IF(org='Communications', 1, 0)) AS ComCount,
    SUM(IF(org='Operations', 1, 0)) AS OpCount,
FROM cost_centers
WHERE cost_center = '$row[13]'

然后你可以通过调用来访问这些值$row["HRCount"];

于 2013-02-27T23:08:01.117 回答
1

这是因为您每次都在循环内打印计数器。它必须是这样的:

<?php

while($row = mysql_fetch_array($result_id))
{
    $profile8 = mysql_query("
        SELECT org
        FROM cost_centers
        WHERE cost_center = '$row[13]'")
    or die(mysql_error());

    $anymatches=mysql_num_rows($profile8);
    while($pro8 = mysql_fetch_array($profile8))
    {
        $orgnames = $pro8[0];
        $names[] = $orgnames;
    }
}

$newArray = array_count_values($names);

foreach ($newArray as $key => $value) {
    echo "$key - $value <br />";
}

无论如何,对第一行中的每一行进行新查询都不是一个好的选择。您应该更好地学习 SQL,尤其是关于 JOIN 和(在您的情况下)COUNT 运算符。

于 2013-02-27T23:08:28.473 回答
0

试试这个 :

SELECT cost_centers.org, count(*) FROM cost_centers WHERE cost_center = what_you_want GROUP BY cost_centers.org
于 2013-02-27T23:08:41.603 回答
0

你在正确的轨道上。

在while之前制作一个数组:

$org = array();

现在,在 foreach 中,使用 $key 在新数组中创建一个键,然后添加一个

foreach ($newArray as $key => $value) {
    $org[$key] += 1;
}

当您完成 while($row = mysql_fetch_array($result_id)) 循环后,再创建一个 foreach 循环并显示结果。

foreach($org as $name => $count){
    echo $name . " = " . $count . "<br />";
}
于 2013-02-27T23:21:54.213 回答