0

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

4

2 回答 2

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 以反映正确的表。

于 2013-01-05T06:19:12.330 回答
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。

于 2013-01-05T06:37:28.093 回答