计算mysql / php中特定值的总和

时间:2013-01-05 06:09:57

标签: php mysql

我有一个表格,其中有一个字段isClaimed,它只有两个固定值= CLAIMED或NOT CLAIMED。我必须计算每个字段的总数。

仅供参考,假设这是我的表:

name | isClaimed  
Aye  | NOT CLAIMED 
Ian  | CLAIMED  
Jan  | NOT CLAIMED  
Zen  | NOT CLAIMED  
Pom  | CLAIMED  

无人认领的总数:3
索赔总额:2

请查看下面的代码:

<?php 

 $sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro 
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId 
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending

$result2 = mysql_query($sql);  

if($result2 === FALSE) {
die(mysql_error()); // TODO: better error handling
} 

// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {

    $firstname = $row2["Firstname"];
    $lastname = $row2["Lastname"];
    $middlename = $row2["Middlename"];
    $barangay = $row2["BarangayName"];
    $level = $row2["LevelName"];
    $allowance = $row2["Allowance"];
    $isClaimed = $row2["isClaimed"];

?>

<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?>   </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>


<?php
// Exit looping
}

?>

<tr>
<td colspan="4" class="spec">Total of unclaimed allowances</td>
<td></td>
 </tr>

<tr>
<td colspan="4" class="spec">Total of claimed allowances</td>
<td></td>
</tr>

我从这里尝试过教程:http://www.randomsnippets.com/2008/10/05/how-to-count-values-with-mysql-queries/ 但我不能让它在PHP中工作。

2 个答案:

答案 0 :(得分:0)

从您链接的教程....

$sql = "SELECT
SUM(IF(sca.isClaimed = "CLAIMED", 1,0)) AS claimedTotal,
SUM(IF(sca.isClaimed = "NOT CLAIMED", 1,0)) AS notClaimedTotal,
pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName,       
school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId
ORDER BY pro.LevelId, pro.ScholarId";

然后

echo $row2["claimedTotal"];

echo $row2["notClaimedTotal"];

请注意,我使用表 sca 作为isClaimed值,只是猜测...不确定您的表结构,也许您需要更改sca以反映正确的表。< / p>

答案 1 :(得分:0)

<?php 
$claimedCount = 0;
$unclaimedCount= 0;
$sql = "SELECT pro.ScholarId, pro.Lastname, pro.Middlename, pro.Firstname, pro.Address, levels.LevelName, school.SchoolName, barangays.BarangayName, payroll.Allowance, sp.Points, pro.ScholarPointId, sca.isClaimed
FROM scholar_profile as pro 
JOIN scholar_school as school ON pro.SchoolId = school.SchoolId
JOIN levels ON pro.LevelId = levels.LevelId
JOIN barangays ON pro.BarangayId = barangays.BarangayId
JOIN payroll ON payroll.PayrollId = levels.PayrollId
INNER JOIN scholar_points as sp ON pro.ScholarPointId = sp.ScholarPointId
JOIN scholar_claim_allowance as sca ON pro.ScholarId = sca.ScholarId 
ORDER BY pro.LevelId, pro.ScholarId";
// OREDER BY id DESC is order result by descending

$result2 = mysql_query($sql);  

if($result2 === FALSE) {
    die(mysql_error()); // TODO: better error handling
} 

// Start looping table row
while ($row2 = mysql_fetch_array($result2)) {

    $firstname = $row2["Firstname"];
    $lastname = $row2["Lastname"];
    $middlename = $row2["Middlename"];
    $barangay = $row2["BarangayName"];
    $level = $row2["LevelName"];
    $allowance = $row2["Allowance"];
    $isClaimed = $row2["isClaimed"];

?>

<tr>
<td class="spec"><?php echo $lastname.", ".$firstname. " " .substr($middlename, 0,1) . "." ; ?>   </td>
<td><?php echo $barangay; ?></td>
<td><?php echo $level; ?></td>
<td><?php echo $allowance; ?></td>
<td><?php echo $isClaimed ?></td>
</tr>


<?php
if($row2["isClaimed"] == "CLAIMED")
    $claimedCount++;
elseif($row2["isClaimed"] == "NOT CLAIMED")
    $unclaimedCount++;
// Exit looping
}

?>

<tr>
    <td colspan="4" class="spec">Total of unclaimed allowances</td>
    <td><?php echo $unclaimedCount;?></td>
</tr>

<tr>
    <td colspan="4" class="spec">Total of claimed allowances</td>
    <td><?php echo $claimedCount;?></td>
</tr>

注意:我没有检查过您的查询。我刚刚提到了关于获得适合您当前结构的计数的建议。而且,强烈建议开始使用mysqli_ *而不是mysql。