0

我的查询正在运行,但我需要计算从查询创建的列的总数,我使用普通的 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>";
?>
4

2 回答 2

0

一个相当简单的方法是$total_salary在循环之前定义一个新变量(例如)while并将每个薪水添加到它。

所以你会有(代码被截断了一些):

...
$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>";

PS:这是一个很棒的查询!!!:)

于 2012-09-06T14:55:20.597 回答
0

由于您只对一列使用 GROUP BY,请查看 WITH ROLLUP 选项,它将从数据库中再返回一行。

如果您这样做,请在您的代码中留下大量注释,因为未来的开发人员可能不会期待额外的行。

于 2012-09-06T16:48:13.533 回答