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

Counting the occurence of a specific variable in multiple columns

非常感谢你们中任何能帮助我解决这个问题的杰出编码员。我在 mysql/php 方面的编码专业知识有限,但我很固执。

到目前为止: 下面这个成功的查询给出了名为 'zmon' 的企业在一列 'rsmed' 中只有 'severe' 的员工人数,我现在需要从多个列中计算 'severe'业务 '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' 的 table 中多列(rslat、rsmed、rscentral、rselbow)中 'severes' 的数量,用于名为 zmon 的业务。

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

希望这些信息对您来说足够了。

谢谢,保罗

试试这个:

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


?>

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

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']}.";
}

您可以操纵查询以使用 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';
}