单个查询中来自不同表的不同SUM

时间:2019-07-02 04:10:14

标签: mysql sql

我是否可以就此代码寻求帮助,因此从不同的表中获取不同的SUM()似乎在此代码中不起作用。对此我深表歉意,因为我完全忘记了如何做。

仅当我仅将总和用于出勤查询时,它才起作用。

$sql = "SELECT *, 

        SUM(num_hr) AS total_hr, 
        attendance.employee_id AS empid, 
        employees.employee_id AS employee, 
        position.description AS job_description, 
        SUM(deductions.amount) AS total_amount_deduction

        FROM attendance

        LEFT JOIN employees ON employees.id=attendance.employee_id 
        LEFT JOIN position ON position.id=employees.position_id 
        LEFT JOIN deductions ON deductions.deduction_id=employees.deduction_id 

        WHERE date BETWEEN '$from' AND '$to'

        GROUP BY attendance.employee_id

        ORDER BY employees.lastname ASC, employees.firstname ASC";

我希望得到结果,但是在每个结果上显示不同的SUM值。

当我移除SUM(deductions.amount) AS total_amount_deductionLEFT JOIN deductions ON deductions.deduction_id=employees.deduction_id时,效果很好。

0 个答案:

没有答案