Php 如何计算从查询创建的临时列的总和
我的查询正在运行,但是我需要计算从查询中创建的列的总数,我正在使用普通的php脚本来添加累计工作时间的值(货币),这也非常有效。但是我如何才能得到这一列的总和或总数,这应该给我一个数字,在回音行中用作“……这一期间的累计工资是”,请参阅我脚本的最后一行 以下是该脚本的剪辑:Php 如何计算从查询创建的临时列的总和,php,mysql,Php,Mysql,我的查询正在运行,但是我需要计算从查询中创建的列的总数,我正在使用普通的php脚本来添加累计工作时间的值(货币),这也非常有效。但是我如何才能得到这一列的总和或总数,这应该给我一个数字,在回音行中用作“……这一期间的累计工资是”,请参阅我脚本的最后一行 以下是该脚本的剪辑: <?php include("../xxx"); $cxn = mysqli_connect($host,$user,$password,$dbname) or die ("Couldn
<?php
include("../xxx");
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die ("Couldn't connect to server.");
$query = "SELECT
ea.`employee_id`,
e.`employee_surname`,
e.`employee_first_name`,
e.`employee_second_name`,
e.`employee_salary`,
FORMAT((IF((SUM(ea.`empl_attendance_total`))<180,(SUM(ea.`empl_attendance_total`)),180)),1) AS nt,
FORMAT((IF(((SUM(ea.`empl_attendance_total`))-(SUM(CASE WHEN WEEKDAY(ea.empl_attendance_date) > 5 THEN ea.empl_attendance_total END)))<=180,
0,(IF(((SUM(ea.`empl_attendance_total`))-(SUM(CASE WHEN WEEKDAY(ea.empl_attendance_date) > 5 THEN ea.empl_attendance_total END)))>180,
((SUM(ea.`empl_attendance_total`))-(SUM(CASE WHEN WEEKDAY(ea.empl_attendance_date) > 5 THEN ea.empl_attendance_total END)))-180,
0)))),1) AS ot,
FORMAT((IF((SUM(ea.`empl_attendance_total`))>180,
IF((SUM(ea.`empl_attendance_total`))-180>=(SUM(CASE WHEN WEEKDAY(ea.empl_attendance_date) > 5 THEN ea.empl_attendance_total END)),
(SUM(CASE WHEN WEEKDAY(ea.empl_attendance_date) > 5 THEN ea.empl_attendance_total END)),(SUM(ea.`empl_attendance_total`))-180),
0)),1) AS st,
FORMAT((SUM(ea.`empl_attendance_total`)),1) AS total
FROM
empl_attendance ea
JOIN
employee e
ON ea.`employee_id` = e.`employee_id`
WHERE ea.`empl_attendance_date` BETWEEN '$start_date' AND '$end_date'
GROUP BY `employee_id`";
$result = mysqli_query($cxn,$query)
or die ("Couldn't execute query.");
$total_salary = 0;
/* Displays items already in table */
echo "<table><br>
<tr>
<th>Empl No</th>
<th>Empl Name</th>
<th>N/T (1.0)</th>
<th>O/T (1.5)</th>
<th>S/T (2.0)</th>
<th>Total Hrs</th>
<th>Est Salary</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
extract($row);
$sal = ((($employee_salary/180)*$nt)+((($employee_salary/180)*$ot)*1.5)+((($employee_salary/180)*$st)*2));
$salary = number_format($sal, 2, '.', ',');
// add this salary to the total
$total_salary += $sal;
echo "<tr>\n
<td>$employee_id</td>\n
<td>$employee_surname, $employee_first_name $employee_second_name</td>\n
<td>$nt</td>\n
<td>$ot</td>\n
<td>$st</td>\n
<td>$total</td>\n
<td>R $salary</td>\n
</tr>\n";
}
// change the format of the salary variable
$acc_sal = number_format($total_salary, 2, '.', ',');
echo "</table><br>";
echo "Accumulated Salary for the selected period is<b> R $acc_sal<b>";
?>
一种相当简单的方法是在
循环时,在之前定义一个新变量(例如$total_salary
),并将每个薪资添加到该变量中
因此,您可能会(代码被截断了一些):
。。。
$total_salary=0;
while($row=mysqli\u fetch\u assoc($result))
{
摘录(行);
$sal=(($employee\u salary/180)*$nt)+($employee\u salary/180)*$ot)*1.5)+(($employee\u salary/180)*$st)*2);
$salary=number_格式($sal,2,',',',');
//把这个工资加在总数上
$total_salary+=$sal;
echo“…行标记…”;
}
回声“
”;
echo“所选期间的累计薪资为$total_Salary”;
附言:这是一个很棒的问题!!!:) 由于您仅在一列上使用GROUP BY,请检查WITH ROLLUP选项,该选项将从数据库中再返回一行
如果您这样做,请在代码中留下一个巨大的注释,因为未来的开发人员可能不会期望额外的一行。Thanx对于@froddd的帮助,虽然公式给了我一个错误的总数,但修复很简单-我已经用更正更新了我的原始脚本,现在工作正常。Thanx对于@froddd的帮助,虽然这个公式给了我一个错误的总数,但是修复是一个简单的方法——尝试公式化格式化的数字得到了错误的总数,所以我只使用了未格式化的变量($sal),然后用脚本格式化了它。我已经更新了我的原始脚本和更正,现在工作良好。再次感谢你为我指明了正确的方向。。。JayOh是的,我应该注意到,$salary
是格式化的。。。显然需要使用$sal
。不要!我也更新了答案,如果这对你有用的话,请随意接受。嗨@Alain Collins,我从来没有听说过ROLLUP,因此你可以肯定我会研究这个问题,maybee不是我在这里使用的东西,但我可能有地方可以派上用场。谢谢你的反馈。。。杰伊
...
$total_salary = 0;
while($row = mysqli_fetch_assoc($result))
{
extract($row);
$sal = ((($employee_salary/180)*$nt)+((($employee_salary/180)*$ot)*1.5)+((($employee_salary/180)*$st)*2));
$salary = number_format($sal, 2, '.', ',');
// add this salary to the total
$total_salary += $sal;
echo "... row markup ...";
}
echo "</table><br>";
echo "<b> Accumulated Salary for the selected period is $total_salary<b>";