我有一个表,其中有一个字段 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 中工作。