计算多列中特定变量的出现次数

时间:2017-05-21 12:28:11

标签: php mysql count multiple-columns

我会非常感谢你们中任何一位出色的编码员可以帮助我解决这个问题。我在mysql / php中的编码专业知识有限,但我很固执。

到目前为止: 下面这个成功的查询给出了名为“zmon”的企业只有一列“rsmed”中“严重”的员工数量,我现在需要从业务“zmon”的多个列中计算“严重”:

$host="localhost";
$username="user"; 
$password="pass";
$db_name="dbase";
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$query = "SELECT COUNT(*) FROM forearm WHERE business='zmon' AND rsmed = 'severe' "; 

$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "There are ". $row['COUNT(*)'] ." employees severe in rsmed.";
}

我被困在这里: 我需要计算名为“forearm”的表中多列(rslat,rsmed,rscentral,rselbow)中“severes”的数量。

因此,列业务包含业务名称。 同一个企业可以有多行,每行对应他们的不同员工。 其他列(rslat,rsmed,rscentral,rselbow)包含4个变量中的任何一个:不显着,低,中,高和严重。

我希望这对你来说足够了。

谢谢,保罗

3 个答案:

答案 0 :(得分:2)

如果你在计算多少' severes'在不同的列(rslat,rsmed,rscentral,rselbow)中,您可以尝试将查询修改为以下内容:

SELECT COUNT(*) AS employee_count, "rsmed" AS rtype 
FROM forearm WHERE business='zmon' AND rsmed = 'severe'
UNION
SELECT COUNT(*) AS employee_count, "rslat" AS rtype 
FROM forearm WHERE business='zmon' AND rslat = 'severe'
UNION
SELECT COUNT(*) AS employee_count, "rscentral" AS rtype 
FROM forearm WHERE business='zmon' AND rscentral = 'severe'
UNION
SELECT COUNT(*) AS employee_count, "rselbow" AS rtype 
FROM forearm WHERE business='zmon' AND rselbow = 'severe'

然后你现在可以像这样编写你的循环:

while($row = mysql_fetch_array($result))
{
    echo "There are {$row['employee_count']} employees severe in {$row['rtype']}.";
}

答案 1 :(得分:2)

您可以操纵查询以使用SUM(criteria)SUM(IF(condition, 1, 0))分别计算每列。

SELECT 
    SUM(rslat = 'severe') as rslat_count,
    SUM(rselbow = 'severe') as rselbow_count,
    SUM(rsmed = 'severe') as rsmed_count,
    SUM(rscentral = 'severe') as rscentral_count
FROM forearm
WHERE business='zmon'

数据:

| id | business |  rslat | rselbow |  rsmed | rscentral |
|----|----------|--------|---------|--------|-----------|
|  1 |     zmon | severe |  severe | severe |      good |
|  2 |     zmon | severe |  severe |   good |      good |
|  3 |     zmon |   good |  severe |   good |      good |
|  4 |     zmon | severe |  severe |   good |      good |

结果:http://sqlfiddle.com/#!9/093bd/2

| rslat_count | rselbow_count | rsmed_count | rscentral_count |
|-------------|---------------|-------------|-----------------|
|           3 |             4 |           1 |               0 |

然后你可以使用

在php中显示结果
$sentence = 'There are %d employees severe in %s';
while ($row = mysql_fetch_assoc($result)) {
    printf($sentence, $row['rslat_count'], 'rslat');
    printf($sentence, $row['rselbow_count'], 'rselbow');
    printf($sentence, $row['rsmed_count'], 'rsmed');
    printf($sentence, $row['rscentral_count'], 'rscentral');
}

<强>已更新

要获得各列的派生总计,您只需将它们相加即可。

SELECT 
   SUM(counts.rslat_count + counts.rselbow_count + counts.rsmed_count + counts.rscentral_count) as severe_total,
   counts.rslat_count,
   counts.rselbow_count,
   counts.rsmed_count,
   counts.rscentral_count
FROM (
   SELECT 
      SUM(rslat = 'severe') as rslat_count,
      SUM(rselbow = 'severe') as rselbow_count,
      SUM(rsmed = 'severe') as rsmed_count,
      SUM(rscentral = 'severe') as rscentral_count
   FROM forearm
   WHERE business='zmon'
) AS counts

结果http://sqlfiddle.com/#!9/093bd/10

| severe_total | rslat_count | rselbow_count | rsmed_count | rscentral_count |
|--------------|-------------|---------------|-------------|-----------------|
|            8 |           3 |             4 |           1 |               0 |

然后显示严重的总数

$sentence = 'There are %d employees severe in %s';
while ($row = mysql_fetch_assoc($result)) {
    printf($sentence, $row['rslat_count'], 'rslat');
    printf($sentence, $row['rselbow_count'], 'rselbow');
    printf($sentence, $row['rsmed_count'], 'rsmed');
    printf($sentence, $row['rscentral_count'], 'rscentral');
    echo 'business in ' . $row['severe_total'] . ' severe conditions';
}

答案 2 :(得分:1)

试试这个:

<?php

$host="localhost";
$username="user"; 
$password="pass";
$db_name="dbase";

$conn = mysql_connect("$host", "$username", "$password")or die("cannot connect"); 

mysql_select_db("$db_name")or die("cannot select DB");

$query = "SELECT COUNT(*) FROM forearm WHERE business='zmon' AND rsmed = 'severe' "; 

$result = mysqli_query($conn,$query);

if($result){

  // Return the number of rows in result set

  $rowcount=mysqli_num_rows($result);

  printf("Result set has %d rows.\n",$rowcount);
  // Free result set

  mysqli_free_result($result);
  }

mysqli_close($conn);


?>