我的查询正在运行,但我需要计算从查询创建的列的总数,我使用普通的 php 脚本来添加累计工作时间的值(以金钱为单位),这也很有效。但是我如何获得该列的总和或总计,这应该给我一个单一的数字,在回声行中用作“....该期间的累计工资是_ ”,请参见我脚本的倒数第二行。
以下是脚本的片段:
<?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>";
?>