2017-05-21 75 views
3

我會大大apreciate你們任何一個精明的編碼器可以幫助我與此。我在mysql/php中的編碼專長是有限的,但我很固執。計算多列中特定變量的發生

到目前爲止: 以下這個成功的查詢給出了名爲'zmon'的企業中只有'rsmed'列中'嚴重'的僱員人數,現在我需要從多個列中計算'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."; 
} 

我堅持在這裏: 我需要數多列「severes」(rslat,rsmed,rscentral,rselbow)在名爲「前臂」爲企業表上的號碼命名爲zmon。

所以,列業務包含一個業務的名稱。 同一業務可以有多行,每行對應不同的員工。 其他列(rslat,rsmed,rscentral,rselbow)包含4個變量中的任何一個:不顯着,低,中,高和嚴重。

我希望這是足夠的信息給你。

謝謝,保羅

回答

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'; 
} 
+0

如果你想獲得所有狀態的計數,還可以使用不同的查詢。 – fyrye

+0

感謝你。我現在意識到我的問題沒有完全表達。我需要加總前臂手腕的數量。 – Paul

+0

@Paul簡單的調整 - 更新,雖然你也可以通過PHP總計各個列來顯示每個列。 – fyrye

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


?> 
+0

謝謝。這拋出了一個錯誤。我認爲這是在msql/msqli部門的混亂。乾杯,保羅 – Paul

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

謝謝,這工作得很好。很棒...... Paul – Paul