我将非常感谢你们中的任何一位出色的编码人员可以帮助我解决这个问题。我在 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.";
}
我被困在这里: 我需要计算名为 zmon 的业务的名为“forearm”的表中多个列(rslat、rsmed、rscentral、rselbow)中“severes”的数量。
因此,列 business 包含企业名称。 同一企业可以有多行,每行对应于他们的不同员工。 其他列(rslat、rsmed、rscentral、rselbow)包含 4 个变量中的任意一个:不显着、低、中、高和严重。
我希望这些信息对您来说足够了。
谢谢,保罗
最佳答案
您可以操纵查询以使用 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';
}
关于php - 计算特定变量在多列中的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44096863/